perlmoo

Is there a way to override the constructor in Moo?


I am working on a MooX module that needs to add a wrapper around the constructor.

I've tried method modifies or having the import method directly change *{"${target}::new"} with no effect.

So how can I do this?


Solution

  • Apparently, around does work:

    package MyRole;
    use Moo::Role
    
    around new => sub { ... };
    

    but the role that has the around needs to be consumed after attributes are added, e.g.

    package MyClass;
    use Moo;
    
    has attr1 => (... );
    with 'MyRole';