You can do a lot with(1) functions in Perl. If you need a lot of similar one, you don't even have to write them. You can make Perl to do the work for you on startup(2):

BEGIN { # Auto-magically generate a number of similar functions without ac +tually # writing them down one-by-one. This makes consistent changes much + easier, but # you need perl wizardry level +10 to understand how it works... # # Added wizardry points are gained by this module beeing a parent +class to # all other web modules, so this auto-generated functions are subc +lassed into # every child. my @stdFuncs = qw(prefilter postauthfilter postfilter defaultwebda +ta late_defaultwebdata task loginitem logoutitem sessionrefresh preconnect prerender + lateprerender cleanup authcheck logstart logend logdatadelivery logwebsocket logrequest +finished logstacktrace remotelog sitemap firewall fastredirect); # -- Deep magic begins here... for my $f (@stdFuncs){ #print STDERR "Function " . __PACKAGE__ . "::register_$f will +call add_$f\n"; no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitN +oStrict) *{__PACKAGE__ . "::register_$f"} = sub ($arg1, $arg2) { my $funcname = "add_$f"; confess("No function name specified") unless defined($ +funcname); $arg1->{server}->$funcname($arg1, $arg2); }; } # ... and ends here }

And as you can see, $f from the for loop gets used in the declaration of the subs. In this case, its value actually gets "baked" into the $funcname string.

(1) or "to" functions. Depends on your mood and how much compassion you have when you hear the code auditor scream in pain.

(2) Preferably while wearing ear protectors.

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

In reply to Re^7: Aren't there code refs as well as function refs? (Update: refactoring very large sub with many variables) by cavac
in thread Aren't there code refs as well as function refs? by dd-b

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.