blue_cowdawg has asked for the wisdom of the Perl Monks concerning the following question:

Let's say I have a Moose based module such that:

has 'thing' => ( isa=> "Bit::Vector, is=>"rw", default => sub { return Bit::Vector->new(32); } 1;
and I run a test on it I get
Attribute (orbitBitMap) does not pass the type constraint because: Val +idation failed for 'Bit::Vector' with value undef
I'm sure I'm missing something here... Does the default not do what I think it does? How do I force this to initialize with a new Bit::Vector object?


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Replies are listed 'Best First'.
Re: Moose type constraint failure
by CountZero (Bishop) on Feb 05, 2013 at 22:36 UTC
    It works for me. The module:
    use Modern::Perl; package Testing; use Bit::Vector; use Moose; has 'thing' => ( isa => 'Bit::Vector', is => 'rw', default => sub { return Bit::Vector->new(32); }, ); no Moose; __PACKAGE__->meta->make_immutable; 1;
    And calling it:
    use Modern::Perl; use Testing; use Data::Dump qw/dump/; my $object = Testing->new; say dump($object);
    output:
    bless({ thing => bless(do{\(my $o = 39557684)}, "Bit::Vector") }, "Tes +ting")
    To reproduce your error, I have to initialize the object as follows:
    my $object = Testing->new({thing => undef});

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Moose type constraint failure
by tobyink (Canon) on Feb 05, 2013 at 22:07 UTC

    Are you passing something like thing => undef to your constructor?

    package Bit::Vector { use Moose; } package Bit::Thing { use Moose; has thing => ( isa => "Bit::Vector", is =>"rw", default => sub { return Bit::Vector->new }, ); } Bit::Thing->new; # fine, "thing" gets the default value Bit::Thing->new(thing => undef); # croaks
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Moose type constraint failure
by kcott (Archbishop) on Feb 06, 2013 at 07:45 UTC

    G'day blue_cowdawg,

    Possibly just a typo in posting, but your has 'thing' => (... is missing a closing parenthesis.

    -- Ken

Re: Moose type constraint failure
by blue_cowdawg (Monsignor) on Feb 06, 2013 at 18:20 UTC

    OK... not too sure why, but I ripped out the offending code and re-wrote it and now it passes tests... Had to be a typo somewhere or some such nonsense... Thanks to all that posted replies. My faith in Moose is now restored.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg