Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: How to call a sub-routine ref

by Corion (Pope)
on Oct 22, 2012 at 10:43 UTC ( #1000310=note: print w/ replies, xml ) Need Help??


in reply to Re^2: How to call a sub-routine ref
in thread How to call a sub-routine ref

See the documentation on the arrow operator in perlop.

Otherwise, the right side is a method name or a simple scalar variable containing either the method name or a subroutine reference, and the left side must be either an object (a blessed reference) or a class name (that is, a package name). See perlobj.

In the examples, all things on the left hand side return undef, and Perl will call that subroutine reference with the return value. The documentation could be a bit more clear about that undef will also "trigger" that case.

use strict; use Data::Dumper; my $ref = sub { print Dumper \@_ }; ()->$ref; do{}->$ref; ()->$ref(); do{}->$ref(); __END__ $VAR1 = [ undef ]; $VAR1 = [ undef ]; $VAR1 = [ undef ]; $VAR1 = [ undef ];


Comment on Re^3: How to call a sub-routine ref
Select or Download Code
Re^4: How to call a sub-routine ref
by tobyink (Abbot) on Oct 22, 2012 at 12:35 UTC

    There's actually nothing special about undef. Any scalar will do, including references.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      ... which doesn't make the documentation any less misleading :). I could assume Perl to raise an error if an unblessed reference or a string not specifying a class name appear on the LHS of the arrow operator. Maybe reformulating it as follows makes it match more closely the behaviour:

      Otherwise, the right side is a method name or a simple scalar variable containing the method name, and the left side must be either an object (a blessed reference) or a class name (that is, a package name), see perlobj. As a third case, if the right side is a subroutine reference, the subroutine will be called with the left side unshifted on the parameter list.

        That makes it sound like the left side is only unshifted onto the parameter list in the third cases, but actually it's unshifted in all three cases.

        Also, I'd cut out the talk of classes and just say it needs to be a package name. For example, File::Spec is not a class in the normal sense of the word, but it's a package name that can appear on the left side of the arrow.

        Here's my attempt at explaining ->...

        Otherwise the right side is a bareword method name, or a simple scalar variable containing a method name or coderef. The left side will be evaluated in scalar context and unshifted onto the parameter list, and then the method or coderef will be called. Except where the right side is a coderef, the left side must evaluate to an object (a blessed reference, see perlobj) or a package name - otherwise method resolution (i.e. determining the code to run given a method name) will fail.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000310]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-10-22 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls