[ABE.pm] closure question

Andy Armstrong andy at hexten.net
Fri Aug 8 10:07:17 PDT 2008


On 8 Aug 2008, at 18:04, Faber J. Fedor wrote:

> 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+ 
> +?


$seed++ is $seed - ++ after the scalar is a postincrement - it returns  
the current value then increments it. If you want preincrement do ++ 
$seed instead.

-- 
Andy Armstrong, Hexten





More information about the ABE-pm mailing list