Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 44639
Status: resolved
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: grehom [...] ntlworld.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



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.
Subject: Re: [rt.cpan.org #44639] Type Constraint example doesn't work
Date: Sat, 28 Mar 2009 09:23:10 -0500 (CDT)
To: Graham Smith via RT <bug-Moose [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Sat, 28 Mar 2009, Graham Smith via RT wrote: Show quoted text
> package User; > use Moose; > use Moose::Util::TypeConstraints; > has 'age' => ( > is => 'rw', > isa => 'NaturalLessThanTwenty', > );
You have to define the types _before_ you use them. Otherwise Moose assumes this is a class name. -dave /*============================================================ http://VegGuide.org http://blog.urth.org Your guide to all that's veg House Absolute(ly Pointless) ============================================================*/
On Sat Mar 28 10:23:26 2009, autarch@urth.org wrote: Show quoted text
> You have to define the types _before_ you use them. Otherwise Moose > assumes this is a class name.
Is it worth the hassle of changing the error message (which people are probably doing various matching on in tests, etc., since there's no core Moose exception classes) in order to make this clearer? e.g. "Validation failed for 'NaturalWhatever' with value 19 (not isa 'NaturalWhatever')"
On Sat Mar 28 23:11:12 2009, HDP wrote: Show quoted text
> On Sat Mar 28 10:23:26 2009, autarch@urth.org wrote:
> > You have to define the types _before_ you use them. Otherwise Moose > > assumes this is a class name.
> > Is it worth the hassle of changing the error message (which people are > probably doing various matching on in tests, etc., since there's no
core Show quoted text
> Moose exception classes) in order to make this clearer? > > e.g. "Validation failed for 'NaturalWhatever' with value 19 (not isa > 'NaturalWhatever')"
Done! This does improve usability, good suggestion hdp. Shawn