New tuits

rafael on 2006-05-03T22:15:19

After some hard times (work related, mainly), I happy to announce I seem to have got some tuits back. Here's what I did this evening, implementing a new keyword, state, which is a Perl 6 thing and that appears in the latest perltodo manpage.

$ bleadperl -Mfeature=state -wle 'sub f { state $x = 42; print $x++ } f; f; f'
42
43
44
This, of course, brand new and subject to change; more about that in David Landgren's next wonderful P5P summary!


Neat - See also

Limbic Region on 2006-05-03T22:46:24

An example of how this might be used in Perl6
#!/usr/bin/pugs
# Demo of the state() variable declaration.
# This is also a neat way of doing OO without actually having OO available.
#
# Please remember to update t/examples/examples.t and rename
# examples/output/cashiers if you rename/move this file.

use v6;

sub gen_cashier () {
    # This variable corresponds to a class variable.
    # It is shared across all "instances" of gen_cashier().
    state $cash_in_store = 0;

    # One could add my() variables here, which correspond to instance variables.
    # These would not be shared.

    # Finally, we return a hashref which maps method names to code.
    return {
        add => { $cash_in_store += $^amount },
        del => { $cash_in_store -= $^amount },
        bal => { $cash_in_store             },
    };
}

my $drawer;
$drawer[$_] = gen_cashier() for 1..3;

$drawer[1]<add>( 59 );
$drawer[2]<del>( 17 );
say $drawer[3]<bal>();  # This should say "42"

Re:Neat - See also

rafael on 2006-05-04T08:07:06

Thanks, I just adapted a version of that to be a regression test in bleadperl (change 28090)