Subject: | TypeConstraints do not observe the user-defined message |
I coded the example from the Moose::Util::TypeConstraints POD. And it
gives me the following error message rather than "This number ($_) is
not less than ten!":
Attribute (a) does not pass the type constraint (NaturalLessThanTen)
with '11' at lib/Moose/Meta/Attribute.pm line 244
Moose::Meta::Attribute::initialize_instance_slot('Moose::Meta::Attribute=HASH(0x66e550)',
'Moose::Meta::Instance=HASH(0xb87900)', 'Testy=HASH(0xb879f0)',
'HASH(0x903460)') called at lib/Moose/Meta/Class.pm line 102
Moose::Meta::Class::construct_instance('Moose::Meta::Class=HASH(0xb7a220)',
'a', 11) called at lib/x86_64-linux/Class/MOP/Class.pm line 336
Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0xb7a220)', 'a',
11) called at lib/Moose/Meta/Class.pm line 81
Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0xb7a220)', 'a',
11) called at lib/Moose/Object.pm line 28
Moose::Object::new('Testy', 'a', 11) called at ./messagebug.pl
line 36
I've attached the code.
Subject: | messagebug.pl |
#!/usr/bin/env perl5.85
#use lib './lib'; I installed in ./lib, commented out for your testing
package Testy;
use Moose;
use Moose::Util::TypeConstraints;
#type 'Num' => where { Scalar::Util::looks_like_number($_) };
subtype 'Natural'
=> as 'Num'
=> where { $_ > 0 };
subtype 'NaturalLessThanTen'
=> as 'Natural'
=> where { $_ < 10 }
=> message { "This number ($_) is not less than ten!" };
coerce 'Num'
=> from 'Str'
=> via { 0+$_ };
enum 'RGBColors' => qw(red green blue);
has 'a' => (isa => 'NaturalLessThanTen');
1;
package main;
use strict;
use warnings;
my $o1 = Testy->new(a => 1);
my $o2 = Testy->new(a => 11);