Thanks Stevan,

I'm impressed by the quick turnaround, and I look forward to trying out the new release.

Charles Alderman
aldermania.com

----- Original Message -----
From: Stevan Little <[EMAIL PROTECTED]>
Sent: Sat, 12 Apr 2008 23:55:24 -0400
Re: Re: attribute w/ coerce and trigger - trigger arguments



Charles,

This bug has been fixed in svn
(http://code2.0beta.co.uk/moose/svn/Moose/trunk/) and will be in the
next release (coming soon, probably this week sometime).

- Stevan

On Apr 9, 2008, at 2:40 PM, Charles Alderman wrote:
Hello,

Just started using Moose (love it). I recently spent a little time struggling through an issue with trigger arguments for a coerced attribute. So, I thought I'd post what I found here in case anyone else runs into it.

Actually, I'm not sure if I found an undocumented feature of Moose, or if it's a bug. The docs say that a trigger is passed an updated value of the attribute. What happens is that if the attribute has a coercion, the argument passed to the trigger hasn't been coerced. So, technically, the argument isn't an updated value. (At least, that's where my confusion came from.)

The coerced value is still available to the trigger through the attribute's accessor, so maybe having the non-coerced value also available is for the best.

For clarification, I included an example.

Thanks,
Charles Alderman
aldermania.com

Versions:
Moose - 0.40
Class::MOP - 0.54

--

From the Moose perldoc:

The trigger option is a CODE reference which will be called after the value of the attribute is set. The CODE ref will be passed the instance itself, the **updated value** and the attribute meta-object (this is for more advanced fiddling and can typically be ignored).

--

Here's an example:
#This example is based on: Schwartz, R.L. "The Moose is Flying (part 2)". LinuxMag Column 95 (Jul 2007)

package Mortgage;

use Moose;
use Moose::Util::TypeConstraints;

use DateTime;
use DateTime::Format::DateManip;
use Date::Manip;
Date_Init( 'TZ=US/Eastern' );

subtype 'DateTime'
 => as 'Object'
 => where { $_->isa('DateTime') };

coerce 'DateTime'
=> from 'Str' => via { DateTime::Format::DateManip->parse_datetime($_) }
 => from 'HashRef' => via { DateTime->new(%$_) };

has 'closing_date' => (
 is => 'rw',
 isa => 'DateTime',
 coerce => 1,

 trigger => sub {
   my ( $self, $val, $meta ) = @_;
   print $self->closing_date->isa('DateTime') ? 'yes' : 'no';  # yes
   print $val->isa('DateTime')                ? 'yes' : 'no';  # no???
 }
);

1;

package main;

my $mtg = Mortgage->new( closing_date => 'yesterday' );
print $mtg->closing_date->isa('DateTime') ? 'yes' : 'no';  # yes





Reply via email to