Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^3: Moose 'clone' Constructor

by huck (Vicar)
on Jul 05, 2018 at 06:25 UTC ( #1217925=note: print w/replies, xml ) Need Help??

in reply to Re^2: Moose 'clone' Constructor
in thread Moose 'clone' Constructor

And, as expected, this didn't work:

but this does

perl -E 'my $x = sub { say "@_" }; &$x("fred")'

as to "XYZ"->$x("fred"), I would not have expected it to work, becuase "XYZ" is not blessed to anything, but seeing that it does, it behaves like i expected, with $thing->method($arg) calling &method as method($thing,$arg)

Replies are listed 'Best First'.
Re^4: Moose 'clone' Constructor
by kcott (Chancellor) on Jul 05, 2018 at 07:34 UTC

    G'day huck,

    Dereferencing the coderef (&$x) I understand. @_ holding the invocant and any other arguments I understand.

    "I would not have expected it to work, ..."

    Me, neither. That's the bit I'm trying to understand.

    — Ken

      Hello kcott,

      From perlop#The-Arrow-Operator (emphasis added):

      "->" is an infix dereference operator, just as it is in C and C++. If the right side is either a [...], {...}, or a (...) subscript, then ....

      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).

      So it appears the Perl parser interprets "XYZ"->$x("fred") as $x invoked with the supposed class/package name "XYZ". I believe this parse option is needed to allow constructors to be called correctly.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        G'day Athanasius,

        Thanks for providing that link. Have a look at the one tobyink provided earlier and the subsequent discussion.

        There appears to be some discrepancy between the perlop and the perlobj documentation. The former says it (i.e. the "XYZ") must be an object or class; the latter doesn't seem to care, inasmuch as a plain old string if fine.

        "I believe this parse option is needed to allow constructors to be called correctly."

        I don't understand what you're referring to here. Could you expand upon this point. Thanks.

        — Ken

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-08-17 09:44 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (176 votes). Check out past polls.