Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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!!!!

Comment on Trapping Error - Delta_Days
Select or Download Code
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!

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); ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://549480]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2015-07-06 08:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls