Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • See http://svn.pugscode.org/pugs/examples/cashiers.pl [pugscode.org]

    # 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-alpha;
     
    sub gen_cashier () {
        # This variable corresponds to a class variable.
        # It is shared across all "instances" of gen_cashier().
        state $cash_in_store = 0;

    • I don't think that it did. Certainly, that example is not a good enough test, because it makes all three calls to gen_cashier before it uses any of the closures, so it makes no difference how many times that assignment of 0 is performed. If I re-write it like this

      use v6-alpha;

      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             },
          };
      }

      gen_cashier()<add>( 59 );
      gen_cashier()<del>( 17 );
      say gen_cashier()<bal>();  # This should say "42"

      then I get the wrong answer:

      $ ./pugs examples/cashiers.pl
      0

      If I remove the assignment of 0, and rely on undef becoming 0:

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

          # 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             },
          };
      }

      gen_cashier()<add>( 59 );
      gen_cashier()<del>( 17 );
      say gen_cashier()<bal>();  # This should say "42"

      then I get back to the right answer:

      ./pugs -w examples/cashiers.pl
      42

      state is doing something though here - a single variable is being shared between all 3 closures. To get the same effect with my I have to hoist my $cash_in_store outside of sub gen_cashier

      In conclusion, I don't think that state in Pugs has regressed - it never worked quite as well the example implied.