Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Help with the concept of closures.

by broquaint (Abbot)
on Jul 06, 2003 at 00:02 UTC ( [id://271700]=note: print w/replies, xml ) Need Help??


in reply to Help with the concept of closures.

Having worked with perl for a reasonable length of time, the concept of a closure should not prove to be so enigmatic.
<plug>Perhaps Closure on Closures would be of help in shedding closures of their enigma.</plug>.
I was hoping some of you could evaluate the code below and tell me what is happening at each stage of program execution.
Here we go then
my $itemInBasket = shoppingList( "sweater" );
Assign $itemBasket the return of shoppingList( "sweater" ) (this much is obvious ;)
my $item = shift();
Create a new lexical in the scope of shoppingList and assign to it the first argument.
return sub { my $otherItem = shift(); print "I need to buy a $item and a $otherItem.\n"; };
Return the reference to a newly created anonymous subroutine.
&$itemInBasket( "pair of shoes" );
Execute the subroutine reference stored in $itemBasket and pass it the string "pair of shows". Now for the tricky bit ...
my $otherItem = shift(); print "I need to buy a $item and a $otherItem.\n";
Assign $otherItem the first argument. Now when we print out $item we are referring to the $item that was created when the anonymous sub was returned. It is still in existence because the anonymous sub 'held on' to $item as it was referred to inside the code (it's not quite as simple as this, but that explanation shall suffice). So the anonymous sub is maintaining the lexical state of the surrounding scope that it was created in.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Help with the concept of closures.
by demerphq (Chancellor) on Jul 06, 2003 at 12:16 UTC

    <plug>Perhaps Closure on Closures (beta) would be of help in shedding closures of their enigma.</plug>.

    Broquaint please do not feel that you need to add <plug> tags to comments like this. The node you wrote is well written, informative and useful. You have every right to feel proud about the node and to refer people to it when necessary. My only beef with the node is that due to implementation problems I feel the term closure has special meaning in Perl and should be restricted to only a class of anonymous subs, however I am aware that I am most likely in the minority with this opinion.

    DigitalKitty should certainly review your node, and if you hadn't provided a link I would have done so myself.

    :-)


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found