Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: Moose 'clone' Constructor

by Athanasius (Archbishop)
on Jul 06, 2018 at 09:44 UTC ( [id://1218034]=note: print w/replies, xml ) Need Help??


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

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,

Replies are listed 'Best First'.
Re^6: Moose 'clone' Constructor
by kcott (Archbishop) on Jul 06, 2018 at 11:09 UTC

    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

      Hello kcott,

      The former says it (i.e. the "XYZ") must be an object or class;

      No, it says an object, or a class name. One might expect that the Perl interpreter would check that the name (i.e., string) supplied refers to an actual class/package, and in the normal case that is what happens, during method lookup. For example, Dog->bark() would be interpreted as a call to the bark method in the Dog class, so if there is no package Dog visible the lookup will result in a runtime error. (Likewise, if there is such a package but it doesn’t contain a bark method.)

      But in the case in question — "XYZ"->$x("fred") — there is no lookup because the method to be invoked is fully specified by the coderef $x. And since there is no need for a method resolution, no check is done to verify that "XYZ" is a valid package/class name; instead, this string is simply passed through as the first argument to the method.

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

      Nothing very profound. :-(  I was thinking of constructors, but I really should have said class methods generally (as opposed to object methods). Since in Perl a class/package is identified only by name, class methods are possible only if the parser recognises a form of method invocation in which the invoker is a class (package) name, i.e., a string. A constructor is just a special case of a class method.

      Hence the "XYZ"->$x("fred") construct needs to be syntactically valid Perl. Whether "XYZ" is verified (necessarily at runtime) as a valid package name is an implementation detail. Choosing to validate class names only when necessary seems like a reasonable implementation choice for the parser.

      Just my 2¢,

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

        On the first point, my "object or class" was only meant as an abbreviation of the quoted documentation, "an object (a blessed reference) or a class name (that is, a package name)". There was no intent to paraphrase and subtly change the meaning: apologies if it came across that way.

        As to the second point, thanks for the explanation. That's clarified what you meant: all good.

        — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-26 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found