DigitalKitty,
Before explaining the code, let me try and explain what a closure is the way I understand it.

A closure is a way to get at data that has gone out of scope. Because the reference count has not reached 0, it persists but is only accessible through the closure.

#!/usr/bin/perl -w use strict; sub shoppingList { my $item = shift(); return sub { my $otherItem = shift(); print "I need to buy a $item and a $otherItem.\n"; }; } my $itemInBasket = shoppingList( "sweater" ); my $newItemInBasket = shoppingList( "lipstick" ); &$itemInBasket( "pair of shoes" ); &$newItemInBasket( "purse" );
Ok - lets take the seemingly simple sub shopping list:
It creates a lexical variable called $item and then returns a sub. The magic comes in that $item should disapear, but it doesn't since the returned sub keeps the reference count from reaching 0. You still use the lexical $item from elsewhere in the program, but you can still get at it through the closure.

Both $ItemInBasket and $newItemInBasket are now code references. They are the referant of the returned anonymous sub, which in turn remembers $item.

When they are de-referenced (I prefer the -> notation), they "remember" the $item and shift the argument list to get $otheritem.

References and closures are the things Perl OO is made of and that is the next logical step.

If you would like more information - let me know. I am sure there will be far better answers than mine anyway. I should point out that you assume the subs will be called with at least one argument. I know this is only for learning purposes, but coding for unexpected input will save you a great deal of troubleshooting time in the long run.
Cheers - L~R


In reply to Re: Help with the concept of closures. by Limbic~Region
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.