Skip Menu |

This queue is for tickets about the DateTime-Format-Builder CPAN distribution.

Report information
The Basics
Id: 3199
Status: resolved
Priority: 0/
Queue: DateTime-Format-Builder

People
Owner: spoon [...] cpan.org
Requestors: matthew [...] mcgillis.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Date: Sat, 9 Aug 2003 12:25:02 -0700
To: spoon [...] cpan.org
From: Matthew McGillis <matthew [...] mcgillis.org>
Subject: DateTime::Format::Builder
I'm trying to write a formatter using builder than will accept a number of common Date Times as input. The code bellow handles all the test cases I'm after. However I need it to not croak so that I can take other actions when a bad date is passed. As best I can tell from the documentation the method in my class should take care of it however it isn't so obviously I'm missing something. Any help you can offer would be appreciated. Thanks! package DateTime::Format::Common; use strict; use warnings; use DateTime::Format::Builder( parsers => { parse_datetime => [ {strptime=> '%m/%d/%Y%n%I:%M%p'}, {strptime=> '%Y/%m/%d%n%I:%M%p'}, {strptime=> '%m-%d-%Y%n%I:%M%p'}, {strptime=> '%F%n%I:%M%p'}, {strptime=> '%b%n%d,%n%Y%n%I:%M%p'}, {strptime=> '%m/%d/%Y%n%H:%M'}, {strptime=> '%Y/%m/%d%n%H:%M'}, {strptime=> '%m-%d-%Y%n%H:%M'}, {strptime=> '%F%n%H:%M'}, {strptime=> '%b%n%d,%n%Y%n%H:%M'}, {strptime=> '%m/%d/%Y'}, {strptime=> '%Y/%m/%d'}, {strptime=> '%m-%d-%Y'}, {strptime=> '%F'}, {strptime=> '%b%n%d,%n%Y'} ] } ); sub on_fail { return undef; } 1;
[matthew@mcgillis.org - Sat Aug 9 23:26:32 2003]: [...] Show quoted text
> The code bellow handles all the test cases I'm after. However I need > it to not croak so that I can take other actions when a bad date is > passed.
One way is to use eval {}. You'll see a lot of that in the test suite. [...] Show quoted text
> package DateTime::Format::Common;
Show quoted text
> use strict; > use warnings;
Show quoted text
> use DateTime::Format::Builder( > parsers => { ... } > );
Show quoted text
> sub on_fail { > return undef; > }
Show quoted text
> 1;
The docs say that *subclasses* can override an on_fail. This module is merely using Builder, not subclassing. Perhaps: package DateTime::Format::Common::Builder; use base qw( DateTime::Format::Builder ); sub on_fail { return undef; } 1; package DateTime::Format::Common; use strict; use warnings; use DateTime::Format::Common::Builder( parsers => { parse_datetime => [ {strptime=> '%m/%d/%Y%n%I:%M%p'}, ... {strptime=> '%b%n%d,%n%Y'} ] } ); 1; If you're doing it in just one file, you may need to: package DateTime::Format::Common; use strict; use warnings; DateTime::Format::Common::Builder->import( parsers => { ... } ); This is due to Perl not being aware that DTFCB is already entirely loaded. On reflection, though this may work, it is ungainly. So I'm going to make it easier. But I'm not sure when I'll get around to it. Soon, I hope. Thanks for bringing it up. cheers, -- Iain.
Subject: DateTime::Format::Builder -- new feature as of 0.76
[matthew@mcgillis.org - Sat Aug 9 23:26:32 2003]: [...] Show quoted text
> Any help you can offer would be appreciated.
With 0.76 (fresh on CPAN a few minutes ago), you can now do: package DateTime::Format::Common; use strict; use warnings; use DateTime::Format::Builder( parsers => { parse_datetime => [ [ on_fail => \&on_fail ], {strptime=> '%m/%d/%Y%n%I:%M%p'}, {strptime=> '%Y/%m/%d%n%I:%M%p'}, {strptime=> '%m-%d-%Y%n%I:%M%p'}, {strptime=> '%F%n%I:%M%p'}, {strptime=> '%b%n%d,%n%Y%n%I:%M%p'}, {strptime=> '%m/%d/%Y%n%H:%M'}, {strptime=> '%Y/%m/%d%n%H:%M'}, {strptime=> '%m-%d-%Y%n%H:%M'}, {strptime=> '%F%n%H:%M'}, {strptime=> '%b%n%d,%n%Y%n%H:%M'}, {strptime=> '%m/%d/%Y'}, {strptime=> '%Y/%m/%d'}, {strptime=> '%m-%d-%Y'}, {strptime=> '%F'}, {strptime=> '%b%n%d,%n%Y'} ] } ); sub on_fail { return undef; } 1; Note the option array just in front of all the strptimes. That's all I've changed from your code. I hope that works for you. Please get back to me in either case =) cheers, -- Iain.