All the Perl that's Practical to Extract and Report

Navigation

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
Without JavaScript enabled, you might want to
use the classic discussion system instead. If you login, you can remember this preference.

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

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

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;