Your question has been well-answered. However, I'd like to take this to the next level and introduce you (if you already don't know) to closures.

A closure is an anonymous subroutine that's been created by another subroutine. The canonical example of a closure is a counter generator.

sub create_counter { my $counter = 0; return sub { $counter++ }; } # # # my $counter = create_counter(); while (&$counter < $Some_Value) { &DO_Stuff; }
This seems like overkill, but it allows for two things:
  1. More than one counter at the same time
  2. Customizable counters
If we just do the following ...
sub create_upper_bounded_counter { my ($upper_bound) = @_; my $counter = 0; return sub { $counter++ <= $upper_bound ? 1 : 0 }; } # # # my $bounded_counter = create_upper_bounded_counter(10); while (&$bounded_counter) { &Do_Stuff; }
Hence why closures are sometimes called "function templates". I've used closures as a way to help organize a set of functions that transformed an input in one of three basic ways. It was just that the transformation changed based on a set of definable parameters. (Take the 3rd thing in this instance, the 4th in that instance, but do the exact same thing.) Closures are also used to parse stuff. I believe it was tye (or tilly) that wrote a closure-based HTML parser as an example of functional programming some months back. Really interesting stuff.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


In reply to Take the next step into closures (Re: subs as args) by dragonchild
in thread subs as args 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.