[Israel.pm] Moose question

sawyer x xsawyerx at gmail.com
Wed Apr 4 00:53:30 PDT 2012


Damn!

You beat me to it... >:|

On Wed, Apr 4, 2012 at 9:52 AM, Dotan Dimet <dotan at corky.net> wrote:

>  Found Moose::Util::TypeConstraints via a search, it offers a simple enum
> function.
> The following worked as a one-liner:
>
> use Moose::Util::TypeConstraints;
> enum q{Col}, [qw(red blue green)];
>  no Moose::Util::TypeConstraints;
>
> package bob;
> use Moose;
>  has qw(color is rw isa Col);
>
> package main;
> my $bob = bob->new(color => q{green});
> print $bob->color; # prints "green"
> $bob->color(q{yellow}); # dies
> print $bob->color;
>
> The error you get when you set an invalid value is something like:
> Attribute (color) does not pass the type constraint because: Validation
> failed for 'Col' with value yellow at ...
>
>
> On 04/04/2012 10:08 AM, Dov Levenglick wrote:
>
> I believe (after trying) that this won't work. Either I tried this
> wrong, or else Perl is looking for some module called MyEnum.pm
>
>
> Best Regards,
> Dov Levenglick
>
>
>
> On Wed, Apr 4, 2012 at 09:40, Shmuel Fomberg <shmuelfomberg at gmail.com> <shmuelfomberg at gmail.com> wrote:
>
>  My experience with Moose is nil, but how about something like that:
>
> my %enum = ( A => 0, B => 1, C => 2);
>
> subtype 'MyEnum',
>       as 'Str',
>       where { exists $enum{$_} },
>       message { "Wrong Enum name: $_" };
>
> And then:
> coerce 'MyEnum',
>       from 'Int',
>       via { $enum{$_} };
>
> Shmuel.
>
> On Wed, Apr 4, 2012 at 3:27 PM, Dov Levenglick <dov.levenglick at gmail.com> <dov.levenglick at gmail.com>
> wrote:
>
>  Hi,
> I want to declare a 3 value'd enum equivalent - i.e. (mixing C and perl):
>
> package My::Enum
> use Moose;
>
> typedef enum {A, B, C} enum_t;            // knowledge in C - how do I
> port this concept to Perl?
> has 'b' => (isa => 'enum_t',  is => 'rw');
>
> 1;
>
>
> How would one go about doing that
>
>
> Best Regards,
> Dov Levenglick
> _______________________________________________
> Perl mailing listPerl at perl.org.ilhttp://mail.perl.org.il/mailman/listinfo/perl
>
>  _______________________________________________
> Perl mailing listPerl at perl.org.ilhttp://mail.perl.org.il/mailman/listinfo/perl
>
>  _______________________________________________
> Perl mailing listPerl at perl.org.ilhttp://mail.perl.org.il/mailman/listinfo/perl
>
>
>
> --
> This message has been scanned for viruses and
> dangerous content by *MailScanner* <http://www.mailscanner.info/>, and is
> believed to be clean.
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20120404/d33f870e/attachment-0001.htm 


More information about the Perl mailing list