[sf-perl] Prototypes, List::MoreUtils and subroutine references, OH MY!

George Hartzell hartzell at alerce.com
Fri Mar 29 09:30:48 PDT 2013


Chris Weyl writes:
 > On Thu, Mar 28, 2013 at 5:16 PM, George Hartzell <hartzell at alerce.com>wrote:
 > 
 > > Is this the way it is or am I screwing something up and getting lucky?
 > >
 > 
 > That's the way it is.  Prototypes are only used when a sub is invoked as a
 > function, not from a coderef or method; this make sense in their context of
 > providing the capacity to emulate built in functions.
 > 
 >                               -Chris

Makes sense.  Thanks!

Funny things happen when you take a reference to e.g. map and try the
same trick:

  DB<1> $m = \&map

  DB<2> x $m
0  CODE(0x7ffa00a33520)
   -> &CODE(0x7ffa00a33520) in ???
  DB<3> x map {$_ < 2} (1,2,3)
0  1
1  ''
2  ''
  DB<4> x &$m {$_ < 2} (1,2,3)
syntax error at (eval 26)[/Users/hartzell/perl5/perlbrew/perls/perl-5.16.2/lib/5.16.2/perl5db.pl:646] line 2, near "$m {"
  DB<5> x &$m(sub {$_ < 2}, (1,2,3))
Undefined subroutine &main::map called at (eval 28)[/Users/hartzell/perl5/perlbrew/perls/perl-5.16.2/lib/5.16.2/perl5db.pl:646] line 2.
  DB<6> x $m->(sub {$_ < 2}, (1,2,3))
Undefined subroutine &main::map called at (eval 29)[/Users/hartzell/perl5/perlbrew/perls/perl-5.16.2/lib/5.16.2/perl5db.pl:646] line 2.

But that only surprised me a little bit.

g.


More information about the SanFrancisco-pm mailing list