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

Re: Is it possible to create a sub exclusive to a sub?

by dragonchild (Archbishop)
on Sep 19, 2004 at 02:07 UTC ( [id://392088]=note: print w/replies, xml ) Need Help??


in reply to Is it possible to create a sub exclusive to a sub?

Incidentally, this mechanism is a really neat way to create a recursive subroutine. BrowserUk, AFAIK, was the first to post it on PerlMonks. (I'm just too lazy to link to the post.) Using Fibonacci numbers as the example:
sub fibonacci { my $_fibo; $_fibo = sub { my $v = shift; $v > 1 ? $_fibo->($v - 1) + $_fibo->($v - 2) : 1; }; $_fibo->( @_ ); }

I personally write all my recursive code like this.

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested

Replies are listed 'Best First'.
Re^2: Is it possible to create a sub exclusive to a sub?
by FoxtrotUniform (Prior) on Sep 19, 2004 at 02:27 UTC

    Yes, stacks of nested subs are super-cool. But, uh, why bother writing all recursive code this way? I don't see any gain in efficiency (if anything, creating all those subs might slow things down a tad), and it introduces an extra level of "WTF?" to people reading the code.

    On the other hand, if you're writing tail-recursive code, this is a great way of not exposing an accumulator variable to the rest of the world. For example (untested):

    sub fact { my $fact_tr; # needs to exist before it gets assigned to $fact_tr = sub { my ($v, $r) = @_; return 1 if $v < 2; # oversimplified $fact_tr->($v-1, $v * $r); }; # thanks ihb for pointing out the missing semi $fact_tr->(@_, 1); }
    Also, have a look at Just Another Godel, Escher, Bach hacker.

    Edit: Separated declaration and use of $fact_tr. Thanks ihb!

    --
    F o x t r o t U n i f o r m
    Found a typo in this node? /msg me
    % man 3 strfry

Re^2: Is it possible to create a sub exclusive to a sub? (nasty memory leak)
by ihb (Deacon) on Sep 19, 2004 at 02:28 UTC

    What I don't like about having anonymous subroutines that aren't closures inside subroutines is that the anonymous subroutine is created for each invocation of the surrounding subroutine. In your examples that means that each time &fibonacci is executed, a new recursive subroutine is created. Having anonymous recursive subroutines that aren't closures is even worse. Rephrase: Writing recursively defined subroutines that doesn't use the surrounding lexical scope as lexical anonymous recursive subroutines is even worse, since it creates a "circular closure". $_fibo in our example above is a circular reference and a very bad such since you can't break the circle manually. That's a pretty nasty memory leak.

    I would prefer to define the recursive subroutine once, rewriting your example to

    { my $_fibo; $_fibo = sub { my $v = shift; $v > 1 ? $_fibo->($v - 1) + $_fibo->($v - 2) : 1; }; sub fibonacci { $_fibo->(@_) } }

    ihb

    Read argumentation in its context!

      Aside from the obvious fact that you don't need a recursive routine to do a fibonacci sequence, I'm still wondering why anonymous subs would ever be preferable to named subs. Is there a measurable speed increase when calling a sub through a pointer rather than a name? Why not just do:
      sub fibo { my $inp = shift; $inp > 2 ? &fibo($inp - 1) + &fibo($inp - 2) : 1 }
      (for the sake of simplicity, this has been coded so that the nth term IS the nth term)

      1) 1
      2) 1
      3) 2
      4) 3

      Fair enough - defining the subroutine multiple times could be ... annoying. I'll definitely look at that modification.

      I'm still not certain about the memory leak part of it, especially the part when you say the anonymous sub isn't a closure. Could you explain that more?

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested

        you say the anonymous sub isn't a closure

        Indeed I did. I expressed myself poorly and I've rephrased my post to hopefully better express what I meant.

        (But first, to avoid any confusion: a closure is a subroutine, named or anonymous, that binds its lexical context.)

        A lexical anonymous recursive subroutine is by necessity a closure. What I meant was that the subroutine, if written as a named subroutine, isn't a closure. By making it lexical and anonymous you make it one though, but the only lexical surrounding it is using is its own declaration.

        The memory leak comes from that inside the subroutine, you use $_fibo. That makes $_fibo reference a subroutine that references $_fibo. Thus, $_fibo is a circular reference and refcount will never drop below two and won't be garbage collected.

        This below illustrates what I've just said. On my perl, and probably yours too, the following will print the same for each iteration.

        sub foo { my $foo; $foo = sub { }; print $foo; } for() for 1 .. 5; __END__ CODE(0x1823d78) CODE(0x1823d78) CODE(0x1823d78) CODE(0x1823d78) CODE(0x1823d78)
        This is because $foo gets garbage collected and the memory slot may be is currently is reused. (Since this just happens to be the implementation you can't trust it to always perform the same though.)

        If you change the subroutine to bind $foo you get

        sub foo { my $foo; $foo = sub { $foo }; print $foo; } for() for 1 .. 5; __END__ CODE(0x274f7c) CODE(0x274da8) CODE(0x1823e48) CODE(0x1823e90) CODE(0x1823ed8)
        Here we see that the memory slot isn't being reused.

        It can also be interesting to look at the output of the &Dump subroutine in the Devel::Peek package. (For extra effect, dump the sub after doing differently many recursive calls.)

        I hope I managed to express myself better this time.

        ihb

        Read argumentation in its context!

Re^2: Is it possible to create a sub exclusive to a sub? (Leak free solution)
by ihb (Deacon) on Sep 19, 2004 at 11:44 UTC

    With regards to the memory leak issue in Re^2: Is it possible to create a sub exclusive to a sub? (nasty memory leak) I thought I also should suggest a solution for those recursive subroutines that need to be a closure because it needs access to its lexical surrounding.

    First I want to emphasize that this is only needed when you need the recursive subroutine to have access to the lexical surrounding. I've very rarely needed that, and my personal style if I need a wrapper subroutine &foo (to e.g. supply default arguments to accumulators ) is to is to just name the recursive subroutine that does the work &_foo_rec.

    If you do as in Re: Is it possible to create a sub exclusive to a sub? but using a global datatype you don't create a circular reference. The problem with this solution is that the subroutine won't work right outside the lexical scope it was created in, something that's rarely needed for the problem you're solving above. If you both need to access the lexical surrounding and need to use it outside its lexical surrounding you can do the bit cumbersome

    use vars qw/ $REC /; my $_foo = sub { ... ; ... $REC->(...) ... ; ... ; }; my $foo = sub { local $REC = $_foo; $_foo->(@_); };
    and use $fac however you like from wherever you like. The $_foo subroutine is the actual subroutine but to make it safe and always work you need to first set $REC before calling it. That's what the wrapper subroutine in $foo is for.

    If you don't need to use the recursive subroutine outside its lexical scope you can do any of the following:

    sub foo { no warnings 'redefine'; local *foo = sub { ... ; ... foo(...) ...; ... ; }; return foo(@_); }
    This redefines the subroutine itself and that should be OK since the recursive subroutine probably never wants to use the wrapper subroutine. If this isn't good enough you may use
    use vars qw/ $REC /; sub foo { local $REC = sub { ... ; ... $REC->(...) ...; ... ; }; return $REC->(@_); }
    which is totally safe. As you see, you may have a reused global variable called $REC or whatever that you always use for the local recursive subroutines.

    The last suggestion above brings the thought to a favourite of mine: &_!

    sub foo { no warning 'redefine'; # needed if the recursive sub calls any + other sub local *_ = sub { ... ; ... _(...) ...; ... ; }; return _(@_); }
    To my great joy Larry realized the coolness of having a "current subroutine subroutine" and &_ will automagically be available for all subroutines in Perl 6!

    Update: Added solution for subroutines that both need to access the lexical surrounding and need to be used outside its lexical surrounding.

    ihb

    Read argumentation in its context!

Re^2: Is it possible to create a sub exclusive to a sub?
by BrowserUk (Patriarch) on Sep 19, 2004 at 05:55 UTC

    288339 (again:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found