Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: Help with the concept of closures. by broquaint
in thread Help with the concept of closures. by DigitalKitty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found