<html style="direction: ltr;">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<style>body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
</head>
<body style="direction: ltr;"
bidimailui-detected-decoding-type="UTF-8" bgcolor="#FFFFFF"
text="#000000">
Found Moose::Util::TypeConstraints via a search, it offers a simple
enum function.<br>
The following worked as a one-liner:<br>
<br>
use Moose::Util::TypeConstraints;<br>
enum q{Col}, [qw(red blue green)];<br>
no Moose::Util::TypeConstraints;<br>
<br>
package bob;<br>
use Moose;<br>
has qw(color is rw isa Col);<br>
<br>
package main;<br>
my $bob = bob->new(color => q{green});<br>
print $bob->color; # prints "green"<br>
$bob->color(q{yellow}); # dies <br>
print $bob->color;<br>
<br>
The error you get when you set an invalid value is something like:<br>
Attribute (color) does not pass the type constraint because:
Validation failed for 'Col' with value yellow at ...<br>
<br>
On 04/04/2012 10:08 AM, Dov Levenglick wrote:
<blockquote
cite="mid:CAO4PGFpkJzXFn2OpzyG6T7XjCxthBNi1jBiPxjSVXd1p_PNt7Q@mail.gmail.com"
type="cite">
<pre wrap="">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 <a class="moz-txt-link-rfc2396E" href="mailto:shmuelfomberg@gmail.com"><shmuelfomberg@gmail.com></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">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 <a class="moz-txt-link-rfc2396E" href="mailto:dov.levenglick@gmail.com"><dov.levenglick@gmail.com></a>
wrote:
</pre>
<blockquote type="cite">
<pre wrap="">
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
<a class="moz-txt-link-abbreviated" href="mailto:Perl@perl.org.il">Perl@perl.org.il</a>
<a class="moz-txt-link-freetext" href="http://mail.perl.org.il/mailman/listinfo/perl">http://mail.perl.org.il/mailman/listinfo/perl</a>
</pre>
</blockquote>
<pre wrap="">
_______________________________________________
Perl mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Perl@perl.org.il">Perl@perl.org.il</a>
<a class="moz-txt-link-freetext" href="http://mail.perl.org.il/mailman/listinfo/perl">http://mail.perl.org.il/mailman/listinfo/perl</a>
</pre>
</blockquote>
<pre wrap="">_______________________________________________
Perl mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Perl@perl.org.il">Perl@perl.org.il</a>
<a class="moz-txt-link-freetext" href="http://mail.perl.org.il/mailman/listinfo/perl">http://mail.perl.org.il/mailman/listinfo/perl</a>
</pre>
</blockquote>
<p><br>
</p>
<br />--
<br />This message has been scanned for viruses and
<br />dangerous content by
<a href="http://www.mailscanner.info/"><b>MailScanner</b></a>, and is
<br />believed to be clean.
</body>
</html>