skirnir has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; package MyClass; use Moose; #use Moose::Util::TypeConstraints; has 'somearray' => ( is => 'rw', isa => 'ArrayRef[Int]'); #has 'somearray' => ( is => 'rw', isa => 'ArrayRef'); 1; package main; #use Moose; #use Moose::Util::TypeConstraints; my @array = (1,2); print Dumper(@array); my $stuff = new MyClass(somearray => \@array); print Dumper($stuff);
Instead I get the following error message:
Attribute (somearray) does not pass the type constraint (__ANON__) wit +h 'ARRAY(0x81d3b74)' at /usr/share/perl5/Moose/Meta/Attribute.pm line + 221 Moose::Meta::Attribute::initialize_instance_slot('Moose::Meta: +:Attribute=HASH(0x8451b3c)', 'Moose::Meta::Instance=HASH(0x8451f5c)', + 'MyClass=HASH(0x84520dc)', 'HASH(0x835c518)') called at /usr/share/p +erl5/Moose/Meta/Class.pm line 102 Moose::Meta::Class::construct_instance('Moose::Meta::Class=HAS +H(0x844b9a4)', 'somearray', 'ARRAY(0x81d3b74)') called at /usr/share/ +perl5/Class/MOP/Class.pm line 306 Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0x844b9 +a4)', 'somearray', 'ARRAY(0x81d3b74)') called at /usr/share/perl5/Moo +se/Meta/Class.pm line 81 Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0x844b +9a4)', 'somearray', 'ARRAY(0x81d3b74)') called at /usr/share/perl5/Mo +ose/Object.pm line 26 Moose::Object::new('MyClass', 'somearray', 'ARRAY(0x81d3b74)') + called at moose_test.pl line 26
When commenting out the has 'somearray'.... line and replacing it with the one commented out above,I get the $stuff object I expect.
Can anyone tell me what I am doing wrong here?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Type Constraints in Moose
by FunkyMonk (Bishop) on Apr 06, 2008 at 16:33 UTC | |
by skirnir (Monk) on Apr 06, 2008 at 16:49 UTC | |
by FunkyMonk (Bishop) on Apr 06, 2008 at 23:26 UTC |