Re: [Newb] Mangling parameters, avoiding recursions?

[email protected] (Chris Prather)
Newsgroups perl.moose
Message-ID <CAEP+=B2Uh+uT24wfG5KisfCFTC_BsHdO8MumMmMsdKJkLUFvFA@mail.gmail.com>
On Fri, Jul 8, 2011 at 6:13 PM, Jesse Luehrs <[email protected]> wrote:
> On Sat, Jul 09, 2011 at 12:10:00AM +0200, Ekki Plicht (DF4OR) wrote:
>> Several attributes of my class should be a bool, but get's thrown at with
>> values like
>> 1, Y, Ja, On for true
>> 0, N, No, Nein, Off. '' (empty string) for false.
>>
>> So I thought that an own subtype + coercion should help here:
>> ----------------
>> subtype 'MyBool',
>>      as 'Str',
>>   where { $_ =~ m/^j|^y|1|^on|^n|0|^off|\A\z/i };
>
> The subtype itself should describe the thing you actually want to end up
> with, not the incoming data (the incoming data is already described in
> the coercion). This should be just "subtype 'MyBool', as 'Bool';".

What doy said.

Specifically you are saying that anything that matches the regular
expression is a valid value. Thus any value passed in that matches
will *not* trigger the coercion. By simply subclassing Moose's built
in Bool you're saying that the only valid states are 1, 0, undef, and
the empty string, and then anything that doesn't match those values
will trigger the coercion.

-Chris
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.