I want to use a Moose::Util::TypeConstraints
in my application.
So I define one in my main.pl
main.pl
use Moose::Util::TypeConstraints;
subtype 'mySpecialType'
=> as 'Object'
=> where sub { $_->does('something') };
use noUse;
In the package noUse.pm
are packages used, which use the type constraint
noUse.pm
package noUse;
use Use1;
use Use2;
1;
and my package Use1
and Use2
are working with the type constraint
Use1.pm
package Use1;
use Moose;
has 'object1' => ( is => 'ro', isa => 'mySpecialType' );
1;
Use2.pm
package Use2;
use Moose;
has 'object2' => ( is => 'ro', isa => 'mySpecialType' );
1;
If I run main.pl
I get this error:
The type constraint 'mySpecialType' has already been created in Use1 and cannot be created again in main at /usr/lib/x86_64-linux-gnu/perl5/5.22/Moose/Util/TypeConstraints.pm line 348 Moose::Util::TypeConstraints::subtype('mySpecialType', 'HASH(0x227f398)', 'HASH(0x2261140)') called at main.pl line 10
What causes this error and how can I fix it?
The standard way is to
use
the common code from the libraries where it's needed, not to let it radiate from the main program.MyTypes.pm
Use1.pm
Use2.pm
And
main.pl
becomes justnoUse.pm
stays unchanged.See simbabque's answer for why.