pipe
pipe

Reputation: 737

How to get a diagnostic from Moo when I try to set an unknown attribute in the constructor?

I'm using Perl with Moo. I just spent some time debugging code due to a typo in an attribute name and was wondering if there is a nice way to avoid it. Take this example:

package Fno {
  use Moo;
  has x => ( is=>'ro', default=>0 );
}

# I want this to fail because 'y' is not a known attribute
Fno->new( y=>2 );

The default is there to show that I can't use the required property; it's always fulfilled by the default.

Is there something built-in to Moo that does this, or some commonly used "Tiny" module or extension to help me? I would also want to avoid Moo::sification if possible!

Upvotes: 2

Views: 48

Answers (1)

brian d foy
brian d foy

Reputation: 132914

I think you want MooX::StrictConstructor.

Typically these sorts of systems have hooks that let you adjust arguments before actually constructing the object. As such, almost anything is valid in the call to new.

Upvotes: 7

Related Questions