in reply to Re^6: Aren't there code refs as well as function refs? (Update: refactoring very large sub with many variables)
in thread Aren't there code refs as well as function refs?
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.
|
|---|