Loch: checking attributes by creating an object

I am taking my first steps with Moose and I have the following question. It seems that I can assign attributes that I have not specified in the module. The error message appears if I try to access this attribute. How can I prevent the very assignment of an attribute that was not specified in the module? In the example below, I am setting the age, although I have not specified it in the module. This is tacitly accepted if I don't try to say it. I would like this error message to appear after the -> new statement.

Code:

 #!/usr/bin/perl
 use strict;
 use warnings;

 use 5.012;
 package People;
 use Moose;
 use namespace::autoclean;
 has 'name' => (is => 'rw');
 __PACKAGE__->meta->make_immutable;

 package main;
 my $friend = People->new( name => 'Peter', age => 20 ); # no error.
 say $friend->name;
 say $friend->age; # here comes the error message.

      

Thank!

+3


source to share


1 answer


Try:

use MooseX::StrictConstructor;

      



Which throws an error like this when you move the age into the constructor:

Found unknown attribute(s) passed to the constructor: age ...

      

+5


source







All Articles