On Fri, Feb 26, 2010 at 12:52:44PM -0500, Steve wrote:
> I've moved on to a slightly more complex situation.  The same phone call  
> record contains two different date fields.  One is a string like 'Jan  
> 21, 2010' (incidentally, this date is repeated for each record, as it  
> represents the end of the billing cycle), which I have successfully  
> converted with:
>
> type 'My::Types::Date' => as 'Str'  => where { defined $_ and $_ =~  
> /^\d{8}$/ };
> coerce 'My::Types::Date' => from 'Str' => via { my $d = shift;
>   if ($d =~ /^([a-zA-Z]+) ([0-9]+).*(\d\d\d\d)$/) { ...this code works}
>
> The second situation is complex.  Instead of a full date with year,  
> month, and day of the call, I am given MM/DD.  No year.  I have logic  
> that 'figures out' the year based on the billing cycle end date (above)  
> which I put into &explodeDate.  Being a big fan of perl's debugger, I've  
> identified that by the time the subroutine gets called, I have no  
> reference to either the date, or my object.
>
>   elsif ($d =~ \d\d\/\d\d) {$d = &explodeDate;}
>
> sub explodeDate {
>     my $self = shift; # Nothing here...
> }

Well, you're calling explodeDate as a function, not as a method. Try
$self->explodeDate (or more probably, $self->explodeDate($d), since you
seem to be expecting it to get the date also).

Also, you shouldn't use a leading & to call regular functions, even when
you're not trying to call them as methods... it's equivalent in this
case, but can cause issues in other cases (particularly with functions
with prototypes).

> Is this proper use of coercion, or is there a better way?  If I'm on the  
> right track, how do I get access to my object within 'explodeDate'?

-doy

Reply via email to