Subject: | Type Constraint example doesn't work |
Date: | Sat, 28 Mar 2009 10:40:33 +0000 |
To: | bug-Moose [...] rt.cpan.org. |
From: | Graham Smith <grehom [...] ntlworld.com> |
I loaded the latest version of Moose and Moose::Type::Constraints
(both v0.73) and I am running on a Centos 5.2 Linux box.
All the test run correctly! However this script:
#!/usr/bin/perl
package User;
use Moose;
use Moose::Util::TypeConstraints;
has 'age' => (
is => 'rw',
isa => 'NaturalLessThanTwenty',
);
subtype 'Natural'
=> as 'Int'
=> where { $_ > 0 };
subtype 'NaturalLessThanTwenty'
=> as 'Natural'
=> where { $_ < 20 }
=> message { "This number ($_) is not less than twenty!" };
package main;
use User;
my $me = User->new(
age => '19',
);
print $me->age, " I wish!\n";
gives this error message:
[grehom@localhost moose]$ perl test_user.pl
Attribute (age) does not pass the type constraint because: Validation
failed for 'NaturalLessThanTwenty' failed with value 19 at
/usr/lib/perl5/site_perl/5.8.8/Moose/Meta/Attribute.pm line 411
Moose::Meta::Attribute::initialize_instance_slot('Moose::Meta::Attribute=HASH(0x89a4854)',
'Moose::Meta::Instance=HASH(0x89a4bf0)', 'User=HASH(0x8f0fb88)',
'HASH(0x901ee00)') called at
/usr/lib/perl5/site_perl/5.8.8/Moose/Meta/Class.pm line 193
Moose::Meta::Class::construct_instance('Moose::Meta::Class=HASH(0x8f14160)',
'HASH(0x901ee00)') called at
/usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/Class/MOP/Class.pm
line 328
Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0x8f14160)',
'HASH(0x901ee00)') called at
/usr/lib/perl5/site_perl/5.8.8/Moose/Meta/Class.pm line 157
Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0x8f14160)',
'HASH(0x901ee00)') called at
/usr/lib/perl5/site_perl/5.8.8/Moose/Object.pm line 17
Moose::Object::new('User', 'age', 19) called at test_user.pl line 19
[grehom@localhost moose]$
Apologies if I am wasting your time I am new to object oriented perl,
used perl for a good many years though.