Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: References for ano subs fixed at compile time? (sorta)

by tye (Sage)
on Jun 18, 2013 at 20:03 UTC ( [id://1039645]=note: print w/replies, xml ) Need Help??


in reply to References for ano subs fixed at compile time?

If a sub doesn't close over any lexicals, than I bet that refs to it will be constant. If subs close over lexicals (or represent different code), than the refs can't be constant unless the refs have non-overlapping lifespans.

DB> sub test(&) { my( $c ) = @_; print 0+$c,$/; return $c } DB> ;{ my($a,$b); test {$a}; test {$b} } 14808916 14808916 DB> ;{ my($a,$b); my $c = test {$a}; test {$b} } 15512116 15512804 DB> ;{ my @a; for(1..3){ my $a; push @a, test {$a} } } 15513028 15511044 15513252

- tye        

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

      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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1039645]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found