in reply to Re: References for ano subs fixed at compile time? (sorta)
in thread References for ano subs fixed at compile time?

the last case is the interesting one for me, the coderef of a block at a fixed position (and therefore the same code). I don't care if other blocks get the same ref after this one gets destroyed.

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! =)

UPDATE

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)

Replies are listed 'Best First'.
Re^3: References for ano subs fixed at compile time? (nope)
by tye (Sage) on Jun 18, 2013 at 22:06 UTC
    Nope it doesn't matter if there are closed over variables, unfortunately you loose your bet...

    To quote myself, with emphasis added:

    If subs close over lexicals (or represent different code)

    "sub {1}" and "sub {2}" are two different subroutines, even when you use prototypes to make the "sub" keyword implied rather than explicit.

    - tye        

      You missed the point, the different calls to sub {1} print different refs, even that 1 is always the same code and not a closed over var.

      The reason is that sub {2} reused the first ref and $b inhibits its release!

      DB<161> ;{ my $b; for (1..3){ print sub {1} ; print $b= sub {2} if $ +_==1 } } CODE(0x86c5af0) CODE(0x86c5af0) CODE(0x86c5be0) CODE(0x86c5be0) DB<163> ;{ my $b; for (1..3){ print 0+sub {1} ; print 0+($b= sub {2} +) if $_==1 } } 141319216 141319216 141319552 141319552

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Well, that part is the stupider part. If address X is no longer used, it is obvious that address X might be re-used. If address X gets re-used, it is obvious that it might still be being used and so can't be re-used a third time.

        But, indeed, even completely identical code refs get recreated each time in my version of Perl (so I lose my bet). Which probably means that a closure is generated even if nothing got closed over.

        But each closure contains a pointer to the code that got compiled for that (trivial) sub. Perhaps that is what you want?

        - tye