Hi Chris,

On Sat, Jul 9, 2011 at 00:01, Chris Prather <[email protected]> wrote:
> On Fri, Jul 8, 2011 at 5:04 PM, Ekki Plicht (DF4OR) <[email protected]> wrote:
>
>>> Also, keep in mind that you can use coercions:
>>>
>>> http://search.cpan.org/dist/Moose/lib/Moose/Cookbook/Basics/Recipe5.pod
>>
>> Right, that's where I had an issue before. As a beginner with Moose I
>> abandoned my attempt and went with around modifiers for the time
>> being.
>
> What issues?
>
> Coercions really are the best answer for data scrubbing unless there
> is an overriding issue why they couldn't be used. Overriding issues in
> this case are the exceptional minority.

>From an earlier draft concerning these issues:

Hi.
Playing around with subtypes, just for the sake of learning.

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 };

coerce  'MyBool',
  from  'Str',
   via  { ($_ =~ m/^j|^y|^on|1/i) ? 1 : 0 ; };

has 'isvalid' => (is => 'rw', isa => 'MyBool', default => 0, coerce => 1);
----------------

'isvalid' has no before, after or around modifier applied to.

Still my attribute receives and stores the original strings like 'On', 'N'
etc. instead of just 0 or 1. So the coercion doesn't work. The where clause
does work, tried that with various test inputs.

Where am I wrong?

TIA,
Ekki

Reply via email to