Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Trapping Error - Delta_Days

by Anonymous Monk
on May 15, 2006 at 13:06 UTC ( #549480=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have a code that takes some input from the user on a specific date, but I am kind of lost when the user input is an invalid date like, 2005-02-31, my code crashes because I am using:

use Date::Calc qw(Delta_Days);

My question is on this part of code I have here, how can a trap the error efficiently before it gets to the Delta_days module.
Here is where the problem is:
while (<OLD>) { my $delta_days; my ($line_year, $line_mon, $line_day) = /(\d{4})-(\d{1,2})-(\d{1,2})/; + $delta_days = Delta_Days($line_year,$line_mon,$line_day,$yearto,$month +to,$dayto); next if $delta_days > 0; print NEW $_; }

Is got to be a way to prevent weird dates to come and crash my program.
Thanks a lot!!!!

Replies are listed 'Best First'.
Re: Trapping Error - Delta_Days
by davidrw (Prior) on May 15, 2006 at 13:30 UTC
    Date::Calc has a check_date() method.
    use Date::Calc qw/ Delta_Days check_date /; ... while( ... ){ ... my ($line_year, $line_mon, $line_day) = /(\d{4})-(\d{1,2})-(\d{1,2}) +/ or next # <==== this is probably desired as well, though check +_date will catch (undef,undef,undef) ; if( ! check_date( $line_year,$line_mon,$line_day ) ){ warn "Bad date"; next; } my $delta_days = Delta_Days($line_year,$line_mon,$line_day,$yearto,$ +monthto,$dayto); ... }
Re: Trapping Error - Delta_Days
by ptum (Priest) on May 15, 2006 at 13:17 UTC

    A Super Search indicates that this question has been asked before ... date validation module is probably a good place to start. Good luck! :)

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Trapping Error - Delta_Days
by blazar (Canon) on May 15, 2006 at 13:23 UTC

    In addition to ptum's reply, you may use a block eval to actually trap the error. Check the function documentation to find out more details!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://549480]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2017-03-24 13:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (301 votes). Check out past polls.