in reply to Re: Constant value in anonymous sub
in thread Constant value in anonymous sub
Each closure has a new instance of $i which is mutable, looking at the code I would expect either always the same instance of $i or $i being an alias of an immutable literal... but it's neither of these...
for my $i (1..3) { $numbers{$i} = sub { $i++;print $i }; }
Cheers Rolf
UPDATE:
OK my problem derived from the difference between range operator and comma operator in producing lists:
DB<37> for my $i (1,2,3) { $numbers{$i} = sub { $i++;print $i }; pri +nt \$i} SCALAR(0x9338210)SCALAR(0x92bdc90)SCALAR(0x9338190) DB<38> &{$numbers{1}} Modification of a read-only value attempted at (eval 43)[/usr/share/pe +rl/5.10/perl5db.pl:638] line 2.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Constant value in anonymous sub (closures & aliases)
by doug (Pilgrim) on Feb 19, 2010 at 15:57 UTC | |
by LanX (Saint) on Feb 19, 2010 at 20:29 UTC | |
by doug (Pilgrim) on Feb 23, 2010 at 14:27 UTC | |
by LanX (Saint) on Feb 24, 2010 at 14:56 UTC |