Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by tobyink (Abbot)
on Oct 22, 2012 at 15:34 UTC ( #1000367=note: print w/ replies, xml ) Need Help??


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

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'


Comment on Re^6: How to call a sub-routine ref
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-12-26 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls