[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: Problem with my code for passing block
From:       demerphq <demerphq () gmail ! com>
Date:       2022-01-11 11:53:22
Message-ID: CANgJU+VcTjLMAZsZXMuT0tZZCNzOaOwWSav4bPStNSRqS-Yi0Q () mail ! gmail ! com
[Download RAW message or body]

On Tue, 11 Jan 2022 at 11:35, Jan Kasprzak <kas@fi.muni.cz> wrote:

> demerphq wrote:
> : On Tue, 11 Jan 2022 at 10:18, Yamadaえりな <yamoerina@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/"

[Attachment #3 (text/html)]

<div dir="ltr"><div dir="ltr">On Tue, 11 Jan 2022 at 11:35, Jan Kasprzak &lt;<a \
href="mailto:kas@fi.muni.cz">kas@fi.muni.cz</a>&gt; wrote:<br></div><div \
class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">demerphq wrote:<br> : \
On Tue, 11 Jan 2022 at 10:18, Yamadaえりな &lt;<a \
href="mailto:yamoerina@gmail.com" target="_blank">yamoerina@gmail.com</a>&gt; \
wrote:<br> : <br>
> &gt; So, I would like to ask another question:<br>
> &gt; Is it safe to pass function reference to the caller (mostly it&#39;s the<br>
> &gt; method instantized from a class) in mod_perl development env?<br>
> &gt; Or should I avoid using this style?<br>
> &gt;<br>
> <br>
> Nothing wrong with passing code refs at all. It is common. Some folks<br>
> prefer to pass *named* subroutines instead of anoymous ones. Eg:<br>
> <br>
> $thing-&gt;method(sub { &quot;whatever&quot; });<br>
> <br>
> might turn into<br>
> <br>
> sub whatever {<br>
> &quot;whatever&quot;;<br>
> }<br>
> <br>
> $thing-&gt;method(\&amp;whatever);<br>
> <br>
> this style will produce more comprehensible and easy to debug error<br>
> messages. But tons of folks dont bother.<br>
<br>
(sorry to elaborate further on non-mod_perl topic)<br>
<br>
In my opinion - do _not_ use named subroutines unless it is strictly<br>
necessary.</blockquote><div><br></div><div>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.</div><div>    </div><blockquote \
class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid \
rgb(204,204,204);padding-left:1ex"> Quite on the contrary - anonymous subs are really \
useful.</blockquote><div><br></div><div>I did NOT say they were not  useful. I said \
they produce shitty error messages:<br><br>$ cat <a \
href="http://t.pl">t.pl</a><br>use strict;<br>use warnings;<br>use Carp \
qw(confess);<br><br>sub callit { $_[0]-&gt;(undef) }<br>sub incr { $_[0]++ \
}<br><br>local $SIG{__DIE__}= \&amp;confess;<br>eval {<br>      callit(sub { $_[0]++ \
});<br>      1;<br>} or warn &quot;anonymous sub failed:\n$@&quot;;<br>eval { <br>    \
callit(\&amp;incr);<br>      1;<br>} or warn &quot;named sub \
failed:\n$@&quot;;<br><br><br>$ perl <a href="http://t.pl">t.pl</a><br>anonymous sub \
failed:<br>Modification of a read-only value attempted at <a \
href="http://t.pl">t.pl</a> line 10.<br>  at <a href="http://t.pl">t.pl</a> line \
10.<br>	main::__ANON__(undef) called at <a href="http://t.pl">t.pl</a> line \
5<br>	main::callit(CODE(0x20c7fb8)) called at <a href="http://t.pl">t.pl</a> line \
10<br>	eval {...} called at <a href="http://t.pl">t.pl</a> line 9<br>named sub \
failed:<br>Modification of a read-only value attempted at <a \
href="http://t.pl">t.pl</a> line 6.<br>  at <a href="http://t.pl">t.pl</a> line \
6.<br>	main::incr(undef) called at <a href="http://t.pl">t.pl</a> line \
5<br>	main::callit(CODE(0x20aed58)) called at <a href="http://t.pl">t.pl</a> line \
14<br>	eval {...} called at <a href="http://t.pl">t.pl</a> line \
13<br></div><br></div><div class="gmail_quote">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.</div><div class="gmail_quote"><br></div><div \
class="gmail_quote">cheers,</div><div \
class="gmail_quote">yves<br><div><br></div></div>-- <br><div dir="ltr" \
class="gmail_signature">perl -Mre=debug -e \
&quot;/just|another|perl|hacker/&quot;</div></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic