MooseX::StrictConstructor



examples/Moose/employee02/script/person.pl
use strict;
use warnings;
use v5.10;

use Person;
use Employee;

my $student = Person->new( name => 'Foo' );
say $student->name;        # Foo

my $programmer = Employee->new( name => 'Bar', employer => 'Perl Corp' );
say $programmer->name;     # Bar
say $programmer->employer; # Perl Corp

my $unemployed = Person->new( name => 'Foo', employer => 'Java Corp' );
# Exception:
# Found unknown attribute(s) init_arg passed to the constructor: employer ...

examples/Moose/employee02/lib/Person.pm
package Person;
use Moose;
use Moose::Util::TypeConstraints;
use MooseX::StrictConstructor;

enum 'Person::Type::Sex' => [ qw(f m) ];

coerce 'Person::Type::Sex'
    => from 'Str'
    => via { lc substr($_, 0, 1) };

has 'name'     => (is => 'rw');
has 'birthday' => (isa => 'DateTime', is => 'rw');
has 'sex'      => (isa => 'Person::Type::Sex', is => 'rw', coerce => 1);

1;