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

I'm new to Moose, and am just trying to get a handle on the basics, but I can't get enum to work. I would have thought that these two would have the same result. The subtype works but the enum is letting me set sex as any string. Any hints on what I'm doing wrong?
package Person; use Moose; use Moose::Util::TypeConstraints; subtype 'Gender' => as 'Str' => where { /^(female|male)$/ }; has sex => ( is => 'rw', isa => 'Gender' ); package Person; use Moose; use Moose::Util::TypeConstraints; enum Gender => qw(female male); has sex => ( is => 'rw', isa => 'Gender' );

Replies are listed 'Best First'.
Re: Moose and enum
by FunkyMonk (Bishop) on May 29, 2008 at 08:42 UTC
    Can you show us the code that doesn't fail, because
    package Person; use Moose; use Moose::Util::TypeConstraints; enum Gender => qw(female male); has sex => ( is => 'rw', isa => 'Gender' ); package main; my $M = Person->new( sex => '' ); # fails #$M->sex( 'please' ); # fails

    both fail for me (Uncaught exception from user code: Attribute (sex) does not pass the type constraint because: Validation failed for 'Gender' failed).

    Update:

    $ perl -MMoose -E 'say $Moose::VERSION' 0.44 $ perl -MMoose::Util::TypeConstraints -E 'say $Moose::Util::TypeConstr +aints::VERSION' 0.22 $ perl -v This is perl, v5.10.0 built for x86_64-linux-gnu-thread-multi

    Unless I state otherwise, all my code runs with strict and warnings
      Ok, seems to be something funny going on here.
      my $M = Person->new( name => 'A', sex => 'please' ); print $M->dump;
      produces this output:
      $VAR1 = bless( { 'sex' => 'please' }, 'Person' );
      I guesss it could be a platform/version issue:
      C:\>perl -MMoose -e "print $Moose::VERSION" 0.22 C:\>perl -MMoose::Util::TypeConstraints -e "print $Moose::Util::TypeCo +nstraints::VERSION" 0.13 C:\>perl -v This is perl, v5.8.8 built for MSWin32-x86-multi-thread
        I downloaded Moose version 0.22 from CPAN, but it failed most of its tests.

        I'd try upgrading if I were you


        Unless I state otherwise, all my code runs with strict and warnings