logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

MooX::StrictConstructor - Make your Moo-based object constructors blow up on unknown attributes.

Author

       George Hartzell <hartzell@cpan.org>

Bugs/Oddities

Inheritance
       A class that uses MooX::StrictConstructor but extends another class that does not will not be handled
       properly.  This code hooks into the constructor as it is being strung up (literally) and that happens in
       the parent class, not the one using strict.

       A class that inherits from a Moose based class will discover that the Moose class's attributes are
       disallowed.  Given sufficient Moose meta knowledge it might be possible to work around this.  I'd
       appreciate pull requests and or an outline of a solution.

   Subvertingstrictness
       MooseX::StrictConstructor documents a trick for subverting strictness using BUILD.  This does not work
       here because strictness is enforced in the early stage of object construction but the BUILD subs are run
       after the objects has been built.

   Interactionswithnamespace::clean
       MooX::StrictConstructor creates a "new" method that namespace::clean will over-zealously clean.
       Workarounds include using MooX::StrictConstructor after namespace::autoclean or telling namespace::clean
       to ignore "new" with something like:

         use namespace::clean -except => ['new','meta'];

Description

       Simply loading this module makes your constructors "strict". If your constructor is called with an
       attribute init argument that your class does not declare, then it dies. This is a great way to catch
       small typos.

       Your application can use Carp::Always to generate stack traces on "die".  Previously all exceptions
       contained traces, but this could potentially leak sensitive information, e.g.

           My::Sensitive::Class->new( password => $sensitive, extra_value => 'foo' );

   STANDINGONTHESHOULDERSOF...
       Most of this package was lifted from MooX::InsideOut and most of the Role that implements the strictness
       was lifted from MooseX::StrictConstructor.

   SUBVERTINGSTRICTNESS
       MooseX::StrictConstructor documents two tricks for subverting strictness and avoid having problematic
       arguments cause an exception: handling them in BUILD or handle them in BUILDARGS.

       In MooX::StrictConstructor you can use a BUILDARGS function to handle them, e.g. this will allow you to
       pass in a parameter called "spy" without raising an exception.  Useful?  Only you can tell.

          sub BUILDARGS {
              my ($self, %params) = @_;
              my $spy delete $params{spy};
              # do something useful with the spy param
              return \%params;
          }

       Because "BUILD" methods are run after an object has been constructed and this code runs before the object
       is constructed the "BUILD" trick will not work.

Name

       MooX::StrictConstructor - Make your Moo-based object constructors blow up on unknown attributes.

See Also

       •   MooX::InsideOut

       •   MooseX::StrictConstructor

Synopsis

           package My::Class;

           use Moo;
           use MooX::StrictConstructor;

           has 'size' => ( is => 'rw');

           # then somewhere else, when constructing a new instance
           # of My::Class ...

           # this blows up because color is not a known attribute
           My::Class->new( size => 5, color => 'blue' );

Version

       version 0.011

See Also