On Tue, 11 Jan 2022 at 11:35, Jan Kasprzak <k...@fi.muni.cz> wrote: > demerphq wrote: > : On Tue, 11 Jan 2022 at 10:18, Yamadaえりな <yamoer...@gmail.com> 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.
This is incredibly bad and irresponsible advice to give to a beginner. Especially as *every* program that is written in the imperative style can be expressed using only anonymous subs. The result would be incredibly difficult to maintain and debug, but it would work. Named subs exist for a reason. Heck naming subs is so important there are multiple modules on CPAN exporting utility functions to name your anonymous subs. > Quite on the contrary - anonymous subs are really useful. I did NOT say they were not useful. I said they produce shitty error messages: $ cat t.pl use strict; use warnings; use Carp qw(confess); sub callit { $_[0]->(undef) } sub incr { $_[0]++ } local $SIG{__DIE__}= \&confess; eval { callit(sub { $_[0]++ }); 1; } or warn "anonymous sub failed:\n$@"; eval { callit(\&incr); 1; } or warn "named sub failed:\n$@"; $ perl t.pl anonymous sub failed: Modification of a read-only value attempted at t.pl line 10. at t.pl line 10. main::__ANON__(undef) called at t.pl line 5 main::callit(CODE(0x20c7fb8)) called at t.pl line 10 eval {...} called at t.pl line 9 named sub failed: Modification of a read-only value attempted at t.pl line 6. at t.pl line 6. main::incr(undef) called at t.pl line 5 main::callit(CODE(0x20aed58)) called at t.pl line 14 eval {...} called at t.pl line 13 One tells me the sub incr() threw an error, the other tells me that an anonymous sub did. If that anonymous sub was constructed at a distance in an eval or something debugging exactly what is going wrong can be a total nightmare. cheers, yves -- perl -Mre=debug -e "/just|another|perl|hacker/"