And my test with your code shows that the ref is fix if its not returned:
DB<123> sub test(&) { my( $c ) = @_; print 0+$c,$/; return $c } DB<124> ;{ my @a; for(1..3){ my $a; push @a, test {$a} } } 145187472 145182504 144725160 => "" DB<125> sub test(&) { my( $c ) = @_; print 0+$c,$/} DB<126> ;{ my @a; for(1..3){ my $a; push @a, test {$a} } } 145184216 145184216 145184216 => ""
So the ref is chosen dynamically from a pool of available refs, it can be the last one if it has been released before.
Thx! =)
OK the following code shatters all my hopes that I found an answer to an old question ...
DB<155> sub tst2(&) { my( $c ) = @_; print &$c,":\t",0+$c,$/;return +$c} DB<156> sub tst1(&) { my( $c ) = @_; print &$c,":\t",0+$c,$/;} DB<157> ;{ my $b; for my $a (1..3){tst1 {$a} ; $b=tst2 {42} if $a==1 + } } 1: 145234328 42: 145234328 2: 141318480 3: 141318480
it doesn't matter how tst1() deals with the coderef, any later coderef generation might bind the address and disable it for other use.
> If a sub doesn't close over any lexicals, than I bet that refs to it will be constant.
Nope it doesn't matter if there are closed over variables, unfortunately you loose your bet...
DB<158> ;{ my $b; for my $a (1..3){tst1 {1} ; $b=tst2 {2} if $a==1 } + } 1: 141317840 2: 141317840 1: 141318608 1: 141318608
Cheers Rolf
( addicted to the Perl Programming Language)
In reply to Re^2: References for ano subs fixed at compile time? (nope)
by LanX
in thread References for ano subs fixed at compile time?
by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |