Ignoring the code you posted, aside from taking note of its indication that you might be asking specifically about sub references.....

Why sub references may be useful is really up to the imagination of the programmer, just as why while() {} may be useful. Sub refs are tools for you to put in your tool chest and use as they are found to be useful. In the type of work you do, you may find them to be seldom used. Others may find them extremely useful. A carpenter doesn't often use an oil filter wrench. A mechanic doesn't often use a nail gun. But farmer may use both in the course of his work-month.

If the hats you wear ever include using CGI::Carp, you might be exposed to using a sub-reference to set up your own error-handler. If you ever use File::Find, you use a subref for the "found" callback. If you program user interfaces, or parsers, you'll often use subrefs for event handlers and token handlers. Sub references allow a general solution to be applied to a specific situation.

References in general are a form of abstraction. An array, a hash, a subroutine, a simple scalar holding a value... they're abstraction layers that are very close to the data. When we need to apply a more generalized solution, we use references to those layers to distance ourselves from the data itself.

David Wheeler has been quoted as saying, "Any problem in computer science can be solved with another layer of indirection." References are one form of indirection. Of course the same Wheeler went on to qualify that with, "....Except for the problem of too many layers of indirection. (Thanks Wikipedia, for the quotes.)


Dave


In reply to Re: reference , advantages? by davido
in thread reference , advantages? by dbs

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.