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

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        

  • Comment on Re^3: References for ano subs fixed at compile time? (nope)

Replies are listed 'Best First'.
Re^4: References for ano subs fixed at compile time? (nope)
by LanX (Saint) on Jun 18, 2013 at 22:14 UTC
    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        

        > Perhaps that is what you want?

        I thought I could use the code-ref as reliable identifier for the call position ( not just the line-number caller provides ).

        Something which can be used to emulate position dependent built-ins like the flip-flop operator.

        I tried B::svref_2object but the docs to B are somehow cryptic...

        Cheers Rolf

        ( addicted to the Perl Programming Language)