demerphq wrote:
: On Tue, 11 Jan 2022 at 10:18, Yamadaえりな <[email protected]> wrote:
:
: > So, I would like to ask another question:
: > Is it safe to pass function reference to the caller (mostly it's the
: > method instantized from a class) in mod_perl development env?
: > Or should I avoid using this style?
: >
:
: Nothing wrong with passing code refs at all. It is common. Some folks
: prefer to pass *named* subroutines instead of anoymous ones. Eg:
:
: $thing->method(sub { "whatever" });
:
: might turn into
:
: sub whatever {
: "whatever";
: }
:
: $thing->method(\&whatever);
:
: this style will produce more comprehensible and easy to debug error
: messages. But tons of folks dont bother.
(sorry to elaborate further on non-mod_perl topic)
In my opinion - do _not_ use named subroutines unless it is strictly
necessary. Quite on the contrary - anonymous subs are really useful.
For example, it is not easy to do unintentional closure in the nested
named subroutine (see 'Variable "%s" will not stay shared' in perldiag(1)).
Also, it is possible to pass coderef sort- or map-style. Just use
prototypes:
sub my_foreach(&@) {
my ($code, @vals) = @_;
# ... do some safety checks or whatever local magic ...
for my $val (@vals) {
$code->($val);
}
}
my_foreach { say shift; } qw(foo bar baz);
-Yenya
--
| Jan "Yenya" Kasprzak <kas at {fi.muni.cz - work | yenya.net - private}> |
| http://www.fi.muni.cz/~kas/ GPG: 4096R/A45477D5 |
We all agree on the necessity of compromise. We just can't agree on
when it's necessary to compromise. --Larry Wall