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

I have a moose class with a boolean attribute. When I create a new instance of that class I try to pass either a 0 or 1 (perl boolean vals I thought) I get an error stating this is not a valid value. Either that or I'm misunderstanding the error message.

Here's the package.

package AXField; # Our libraries use lib 'C:\Users\Jay\Desktop\SBS DEV\CODE\perl\Utilities'; use Moose; # Attributes has 'Name' => (is => 'rw', isa => 'Str', required => 1); has 'Value' => (is => 'rw', isa => 'Str', reader => 'Value', writer => + 'SetValue'); has 'isKey' => (is => 'rw', isa => 'Boolean'); has 'Len' => (is=>'rw', isa => 'Num'); has 'DecimalPos' => (is => 'rw', isa => 'Num'); has 'Type' => (is => 'rw', isa => 'Str'); has 'OriginalValue' => (is => 'rw', isa => 'Str'); has 'ParentRec' => (is => 'rw', isa => 'Str', required => 1); has 'Populated' => (is => 'rw', isa => 'Str'); # Contains a single field sub BUILD # Constructor { my $self; $self = shift; $self->Populated(0); $self->OriginalValue(' '); $self->SetValue(' '); } sub Value { my $self; $self = shift; return $self->Value; } sub SetValue { my $self; $self = shift; $self->Value = shift; if ($self=>Populated() eq 'N') { $self=>Populated() eq 'Y'; } } 1;
Here's the line that creates a new "Field"
&Fld = AXField->new(Name => $Prop[0], Value => ' ', isKey => 1, Len => + $Length, DecimalPos => $prec, Type => $Prop[4], ParentRec => $self-> +Name);
We're interested in the isKey attribute.

Here's the error message

Attribute (isKey) does not pass the type constraint because: Validatio +n failed f or 'Boolean' with value 1 (not isa Boolean) at C:\Perl64\lib\Moose\Obj +ect.pm lin e 24
What am I missing

Replies are listed 'Best First'.
Re: Moose boolean type values: Noob
by holli (Abbot) on Oct 22, 2017 at 21:16 UTC
    Not a Moose expert, but shouldn't hat be Bool instead of Boolean?


    holli

    You can lead your users to water, but alas, you cannot drown them.
Re: Moose boolean type values: Noob
by jorba (Sexton) on Oct 22, 2017 at 21:32 UTC
    Colour me stupid. Many thanx