[Israel.pm] Moose question

sawyer x xsawyerx at gmail.com
Wed Apr 4 00:52:34 PDT 2012


There is actually an Enum type for Moose already:
Moose::Meta::TypeConstraint::Enum[1]
(I'm thinking of writing emails in Spinx form.. curse you Meir!)

To use it, you simple write, from the documentation:
    use Moose::Util::TypeConstraints;
    enum 'RGBColors', [ qw(red green blue) ];

I would personally write it as such:
    enum RBGColors => [ qw/red green blue/ ];

I think it's more readable. :)

The enum type constraint is documented here:
https://metacpan.org/module/Moose::Util::TypeConstraints


[1] https://metacpan.org/module/Moose::Meta::TypeConstraint::Enum



On Wed, Apr 4, 2012 at 9:08 AM, Dov Levenglick <dov.levenglick at gmail.com>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>
> 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
> >
> > 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 list
> >> Perl at perl.org.il
> >> http://mail.perl.org.il/mailman/listinfo/perl
> >
> >
> >
> > _______________________________________________
> > Perl mailing list
> > Perl at perl.org.il
> > http://mail.perl.org.il/mailman/listinfo/perl
> _______________________________________________
> 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/8b55a946/attachment.htm 


More information about the Perl mailing list