[ABE.pm] closure question

Faber J. Fedor faber at linuxnj.com
Fri Aug 8 10:04:49 PDT 2008


Take your canonical closure example:

sub plusplus {
    my $n = shift;
    return sub { $n++ ;}
}

my $seed = 1;
my $c = plusplus($seed);

print $c->()."\n";
print $c->()."\n";

$seed = 100;
my $d = plusplus($seed);
print $d->()."\n";
print  $d->()."\n";

The output of which is, naturally,

1
2
100
101


I understand the 2 and the 101.  I don't understand the 1 and the 100.
Why does the first call to the closure result in $seed and not $seed++?



-- 
 
Regards,
 
Faber Fedor
President
Linux New Jersey, Inc.
908-320-0357
800-706-0701

http://www.linuxnj.com





More information about the ABE-pm mailing list