in reply to Rename a Moo constructor from "new" to "run"

I guess: If you want a different implementation a role with a method modifier (around) might be a way to go. I’m not sure. Unfortunately i can‘t provide an example because i‘m about to set up my new machine. Regards, Karl

Update:

Probably it might be interesting for you what i wrote a while ago to illustrate how to overwrite a constructor using inheritance using Class::Tiny. You may call it plugin, interface, fubar or what ever you like.

I hope it is helpful and inspiring. And doesn’t look sub run { shift->SUPER::new(@_) } cool? 😎

«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^2: Rename a Moo constructor from "new" to "run"
by tobyink (Canon) on Mar 30, 2019 at 20:05 UTC

      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

        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.

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