Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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 a soft breeze sighs...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-06-22 02:29 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.