Let me try to give you a different perspective on anonymous subs than you've gotten so far. (Not that what's been written is bad, wrong, or inaccurate; it's all very good stuff. But it's not the whole story.)

Perl allows you to treat functions as first-class data objects -- you can pass them to other functions, return them, store (references to) them, and generate them on the fly. This is difficult to handle with the usual symbol table mechanism (named functions that you call just like builtins), so Perl lets you store (refs to) functions in scalars. This is where you get anonymous functions: functions that aren't in the symbol table.

Here's an example: suppose you're turning Usenet-style text (*bold*, _italics_,, etc) into HTML-style markup. One way to do it would be to write a bunch of conversion functions:

sub translate_bold { ... } sub translate_italics { ... } ...
but that gets tedious pretty quick. Another way (and I'm not saying it's the best way, but it illustrates the point) is to write a function to generate the conversion functions:
sub build_trans { my ($usenet, $tag) = @_; return sub { my ($text) = @_; # do translation here, using $usenet and $tag } }
Then you can keep all the translation patterns in a config file, and build your translation functions on the fly:
while (my $pat = &read_trans_pattern($cfg)) { $trans_funcs{$pat->{'usenet'}} = &build_trans_func($pat->{'usenet'}, $pat->{'tag'}); }
Want to add support for +monospace text+? Thanks to your translation-function builder, all you have to do is add a line to your config file.

This may seem unnecessarily complex, but what it does is keep the complexity in a small part of your code -- the translation-function builder -- rather than spread it out over dozens of functions.


In reply to Re: Anonymous Subroutines by FoxtrotUniform
in thread Anonymous Subroutines by Anonymous Monk

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.