in reply to Re^5: Rename a Moo constructor from "new" to "run"
in thread Rename a Moo constructor from "new" to "run"

Omitting the pedantic: "Does $self->new make sense?", does Perl have something similar to new XYZ; which will search for the nearest new() all the way up the inheritance tree of XYZ?

5' Edit: perhaps Moo supplies these

Replies are listed 'Best First'.
Re^7: Rename a Moo constructor from "new" to "run"
by AnomalousMonk (Archbishop) on Mar 31, 2019 at 03:33 UTC

    I'm not sure what choroba said in response to this question, and these comments are in the context of general Perl OO practice and not Moo, but ...

    ... "Does $self->new make sense?" ...

    I sometimes see constructor code that looks something like

    sub new { my $class_or_objref = shift; my $ref_class = ref $class_or_objref; # true if an object referen +ce my $object_reference; if ($ref_class) { # build object reference from existing object attributes # and supplied @_ arguments. } else { # build object reference for class from supplied @_ arguments. } return $object_reference; }
    To me, this factors naturally as
    sub new { my $classname_or_objref = shift; return ref $classname_or_objref ? $classname_or_objref->new_clone + (@_) : $classname_or_objref->new_pristi +ne(@_) ; }
    (Update: From this point on, @_ | no, changed my mind, will use ... (yada) for this notation represents a general argument LIST, possibly empty.)
    So every class/package may potentially need both a
        $object_reference->new_clone(...)
    method that builds and returns a new object reference based on the attributes of an existing object and any additional arguments supplied, and a traditional
        ClassName->new_pristine(...)
    method that builds and returns a new object reference based solely on a class name bareword/string and any arguments. It's immediately obvious that
        ClassName->new_pristine(...)
    looks a lot like the very familiar
        ClassName->new(...)
    usage, and
        $object_reference->new_clone(...)
    can very intuitively be represented simply as
        $object_reference->clone(...)

    IMHO, there is no value added by trying to combine object clone creation with "classic" object construction. The calls
        my $obj = ClassName->new(...);
    and
        my $obj = $existing_object_reference->clone(...);
    should do exactly what you expect them to do and no more. A  ClassName->new constructor definition can often be very short, sweet and easy to understand; don't clutter it up with unnecessary stuff. If no  clone() constructor is needed for a class (and it usually isn't), don't define one. So, in answer to the question,  $self->new can make sense, but usually doesn't, and certainly not with that method name.

    ... does Perl have something similar to new XYZ; which will search for the nearest new() all the way up the inheritance tree of XYZ?

    IIRC, any  ClassName->method() or  $objref->method() call will search for method() first in the specified class (either explicitly named or embedded in the object reference) and then all the way up the @ISA tree of the specified class under the control of the mro (available with Perl version 5.10+) currently selected, and then into the UNIVERSAL class. (And, of course, new has the same status as any other method name. It is used as a constructor name by convention; any other — well documented! — name could be used.)


    Give a man a fish:  <%-{-{-{-<

Re^7: Rename a Moo constructor from "new" to "run"
by choroba (Cardinal) on Mar 30, 2019 at 23:39 UTC
    Sorry, my fault, fixed. It's too late here.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]