http://qs1969.pair.com?node_id=939716

sundialsvc4 has asked for the wisdom of the Perl Monks concerning the following question:

I have this ...

package myTypes; use MooseX::Types -declare => [ qw( myOffset ) ]; ... subtype myOffset, as Int, where { ($_ >= 0) && ($_ <= 99) }, message { "offset '$_' is invalid (out of range)."} ;
and in the object ...
use myTypes qw( myOffset ); ... has 'my_offset' => ( is => 'rw', isa => 'MyOffset', default => 0, );

And when I instantiate the object:

Attribute (my_offset) does not pass the type constraint because: Valid +ation failed for 'MyOffset' with value 0 at constructor ...

Changing it to isa => 'Int', for example, allows the constructor to continue.   I have confirmed that is_MyOffset(0) is true.   All of the edge-cases work.

On closer look, it appears that any use of a isa=>'some_subtype' is refusing to accept the default value even when the where clause for some_subtype says that it should.   (For example, taking a definition PositiveInt straight out of the Synopsis of MooseX::Types, the message will appear if the default is 1.

Fudging the isa string to produce a bogus type-name fails immediately (as is correct), and removing the where clause from the existing subtype removes the problem.   So it certainly would appear that the subtype, and its associated where clause, is being found and executed.

Edit:   On closer inspection of the documentation (what? what%squo;s that?) for MooseX::Types, I see that they recommend using barewords for the types, noting also that this will cause compile-time error goodness.   (MooseX::Types::Moose provides the standard Moose types.)   Making this change resolved the problem, although I remain somewhat fuzzled as to why it occurred.)