Hi, today I've been dabbling with Moose for the first time. After reading the doc for Moose::Util::TypeConstraints, I would have expected this example to result in $stuff to be instantiated with [ 1, 2 ] in 'somearray':

#!/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?


In reply to Type Constraints in Moose by skirnir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.