Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Rename a Moo constructor from "new" to "run"

by karlgoethebier (Abbot)
on Mar 30, 2019 at 20:25 UTC ( [id://1231906]=note: print w/replies, xml ) Need Help??


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

Thanks. But are you sure? Just because i still can't test it. I vaguely remember that it only worked with the SUPER::. Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Replies are listed 'Best First'.
Re^4: Rename a Moo constructor from "new" to "run"
by tobyink (Canon) on Apr 01, 2019 at 15:58 UTC
    use v5.16; package Foo { use Moo; has blah => (is => 'ro', required => 1); sub run { shift->new(@_) } } say Foo->run(blah => 42)->blah;

    Doing SUPER::new will probably work in this case, though if you call it repeatedly, you won't benefit from some of the optimizations Moo does. (Moo doesn't actually built a Foo::new sub straight away, but when Moo::new gets called, it will notice that Foo::new is missing, build an optimized constructor, install it, and goto it.)

    In cases where you weren't using a class building toolkit like Moo, and Foo::new was a hand-written constructor, then you definitely wouldn't want to call SUPER::new because you'd be bypassing the class's hand-written constructor (in favour of the superclass's constructor) which might be doing important stuff.

      Pretty cool. Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re^4: Rename a Moo constructor from "new" to "run"
by bliako (Monsignor) on Mar 30, 2019 at 22:59 UTC

    SUPER::new() calls new() of parent-class, if any.

      So does $self Class->new if no sub new was declared in the Class.

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

        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

      Sure. That's why i did it in the example mentioned: Inheriting the constructor, renaming it and changing it's behavior. A plugin...

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found