I've only seen (and used) this in a potential test&debug setting, like so

my $function = sub { ... }; if($isDebug) { $function = sub { ... } }

Depending on the scope and visibility of $function, it might be possible to replace this "function pointer" from the outside. This could be used to either change the algorithm, make custom callbacks or, again, for debugging. For example, it might be useful to provide pre-selected "random" dice rolls when testing a game engine.

my $getDiceRoll = sub { return int(rand() * 6) + 1; }; my $dicefh; if($regressionTest) { $getDiceRoll = sub { # READ NEXT DICE ROLL FROM dicerolls.txt if(!defined($dicefh)) { open($dicefh, '<', 'dicerolls.txt') dir die($!); } my $roll = <$dicefh>; chomp $roll; return $roll; } }

The nice thing about this, compared to the usual way of adding a bunch of IFs into the sub is that, well, you don't. Depending on what the function does and how often it it called, this may have relevance to the performance of the function.

Just imagine a graphics library that provides a getpixel() function. If it has to check every time you read out a pixel if it's in indexed or RGB mode while working on a big image, this can get awfully slow (see Autoclicker! Cheating is winning, too! for a practical example where performance matters). One way to potentially speed up things would be to overwrite the getpixel() function with the version optimised for the image that is currently being worked on. It's a bit higher on startup cost, but could save time overall when reading large portions of the pixel data.

Edit: Bugfix second code snippet. Thanks, johngg for the extra set of eyes!

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

In reply to Re: Anonymous subroutines by cavac
in thread Anonymous subroutines by Bod

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.