For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2008 > Time::Piece capturing parsing problems









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Time::Piece capturing parsing problems
Myf White

2008-02-28, 4:05 am

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.
$



--
Myf White

Work like you don't need the money. Love like you've never been hurt. Dance
like nobody's watching.
~ Satchel Paige

Gunnar Hjalmarsson

2008-02-28, 10:03 pm

Myf White wrote:
> 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.


I for one think it's a result of how Time::Piece works...

One option might be to use 'good old' Date::Parse instead:

$ cat test.pl
use Date::Parse;
my @tests = ('28 FEB 2008', 'garbage 28 FEB 2008',
'28 FEB 2008 garbage');
foreach my $i ( 0..$#tests ) {
print "TEST $i\n";
if ( my ($d, $m, $y) = ( strptime $tests[$i] )[3..5] ) {
printf "%02d/%02d/%d\n", $d, $m+1, $y+1900;
} else {
warn "Parsing of '$tests[$i]' failed";
}
}
$ perl test.pl
TEST 0
28/02/2008
TEST 1
Parsing of 'garbage 28 FEB 2008' failed at test.pl line 8.
TEST 2
Parsing of '28 FEB 2008 garbage' failed at test.pl line 8.
$

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Paul Lalli

2008-02-28, 10:03 pm

On Feb 28, 2:22=A0am, myfwh...@gmail.com (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 b=

e
> 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 strin=

g
> in strptime: ...") just goes to the screen - but I need to be able to hand=

le
> it.
>
> This is a snippet of code to show what I mean:
>
> # # # # # # #
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> use Time::Piece;
>
> my @tests =3D ( '28 FEB 2008 stuff at the end to see what Time::Piece does=

',
> 'blah 28 FEB 2008' );
> my $format =3D '%d %h %Y';
>
> foreach my $i ( 0..$#tests ) {
> =A0 =A0 print "\nTEST $i\n";
> =A0 =A0 eval {
> =A0 =A0 =A0 =A0 my $date_object=3D Time::Piece->strptime($tests[$i], $form=

at);
> =A0 =A0 =A0 =A0 print $date_object->dmy("/") . "\n";
> =A0 =A0 };
> =A0 =A0 if ($@) {
> =A0 =A0 =A0 =A0 print "eval error: $@";
> =A0 =A0 }}
>
> # # # # # # #
>
> The output from running this is:
>
> $ perl test.pl
>
> TEST 0
> garbage at end of string in strptime: =A0stuff 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__} =3D sub { die @_ };
my $date_object=3D Time::Piece->strptime($tests[$i], $format);
print $date_object->dmy("/") . "\n";
};


Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com