Re: Time::Piece capturing parsing problems
- From: mritty@xxxxxxxxx (Paul Lalli)
- Date: Thu, 28 Feb 2008 09:54:07 -0800 (PST)
On Feb 28, 2:22 am, myfwh...@xxxxxxxxx (Myf White) wrote:
Hi everyone,
I think my question relates to STDOUT rather than Time::Piece but I'm not
sure.
I am trying to use Time::Piece to process and convert a string which may be
a bit dodgy. What I can't understand is how to capture the problem. The
following code only captures the problem with the second test in the
$EVAL_ERROR ($@). The problem with the first one ("garbage at end of string
in strptime: ...") just goes to the screen - but I need to be able to handle
it.
This is a snippet of code to show what I mean:
# # # # # # #
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
my @tests = ( '28 FEB 2008 stuff at the end to see what Time::Piece does',
'blah 28 FEB 2008' );
my $format = '%d %h %Y';
foreach my $i ( 0..$#tests ) {
print "\nTEST $i\n";
eval {
my $date_object= Time::Piece->strptime($tests[$i], $format);
print $date_object->dmy("/") . "\n";
};
if ($@) {
print "eval error: $@";
}}
# # # # # # #
The output from running this is:
$ perl test.pl
TEST 0
garbage at end of string in strptime: stuff at the end to see what
Time::Piece does at C:/Perl/site/lib/Time/Piece.pm line 470.
28/02/2008
TEST 1
eval error: Error parsing time at C:/Perl/site/lib/Time/Piece.pm line 470.
$
Looks to me like Time::Piece is just printing a warning rather than
die()ing on that particular error. I'd suggest simply turning
warnings into errors for the duration of your eval{ }:
eval {
local $SIG{__WARN__} = sub { die @_ };
my $date_object= Time::Piece->strptime($tests[$i], $format);
print $date_object->dmy("/") . "\n";
};
Paul Lalli
.
- References:
- Time::Piece capturing parsing problems
- From: Myf White
- Time::Piece capturing parsing problems
- Prev by Date: Re: Time::Piece capturing parsing problems
- Next by Date: Re: Data file from one format to another.
- Previous by thread: Re: Time::Piece capturing parsing problems
- Next by thread: validating xml file with xsd
- Index(es):
Relevant Pages
|