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

G'day tobyink,

++ Thanks for pointing me to that module: I hadn't encountered it previously.

I found the way you've used coderefs interesting. I suspect I may have seen something similar in the past but probably wasn't paying close enough attention. I'm certainly familiar with:

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

And, as expected, this didn't work:

$ perl -E 'my $x = sub { say "@_" }; $x("fred")' syntax error at -e line 1, near "$x(" Execution of -e aborted due to compilation errors.

However, I've never written code like:

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

I'll have to look into that further. I've read the "Object::Util - Rationale" section. Can you point me to any documentation that explains this in more detail?

— Ken

Replies are listed 'Best First'.
Re^3: Moose 'clone' Constructor
by tobyink (Canon) on Jul 05, 2018 at 08:29 UTC

      ++ Many thanks. That shines a light on all the grey areas, including (from my earlier post):

      "XYZ"->$x("fred")

      being exactly equivalent to

      $x->("XYZ", "fred")

      — Ken

        Assuming $x is a coderef, then yes, they're exactly equivalent. I don't think you can even detect the difference in the Perl OP tree.

        But if $x is a string (containing a method name, optionally qualified with a package name), then $thing->$x("fred") will work if $thing is a package name (string) or a blessed object, but won't work otherwise.

Re^3: Moose 'clone' Constructor
by huck (Prior) on Jul 05, 2018 at 06:25 UTC

    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)

      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,