The most obvious uses have already been pointed out, but I'd like to mention one other: closures.

When an anonymous subroutine holds references to a lexically scoped variable, and a reference to the subroutine is kept after the lexical variables pass out of scope, the result is a closure: those lexical variables are now held only by that anonymous sub. You can create an entire object system this way, if you are so inclined, or you can just pass around coderefs that hold state. This allows a routine to have a persistent local variable that keeps its value between calls. Further, if the code that creates the closure gets called multiple times, it creates multiple closures -- each with its own variables. Unfortunately, I don't have a really good example of this to give you, because all the nodes I've posted so far that use this technique are obfuscated and so don't make very clear examples. Sorry about that. But it is possible to use this same effect in much cleaner, nicer, better-looking, and more maintainable code. Maybe someone else can give you a good legible example.


$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

In reply to Re: Anonymous Thingies by jonadab
in thread Anonymous Thingies by Sprad

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.