Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comedy of StdErrs

by abitkin (Monk)
on Aug 11, 2003 at 15:39 UTC ( #282905=perlquestion: print w/replies, xml ) Need Help??

abitkin has asked for the wisdom of the Perl Monks concerning the following question:

I have some code:
use Time::Local; ... my $ref = shift; my $date1 = shift @$ref; my $time1 = shift @$ref; print "-1\n" and exit unless $date1 =~ /(\d\d)(\d\d)(\d\d)/; my @date2 = ($1,$2,$3); print "-1\n" and exit unless $time1 =~ /(\d\d):(\d\d)/; my @time2 = ($1,$2); my $t; eval {$t = timegm(0,$time2[1],$time2[0],$date2[1],$date2[0]-1,$dat +e2[2]+100)}; print "-1\n" and exit if $@; ...

Is this the only way to find if there is an error generated? I need to spit out my own error code for every error case in this application. Or is there another way to catch the error? If, for example, the user enters, let's say, Febuary 31st or some other logically wrong value.
==
Kwyjibo. A big, dumb, balding North American ape. With no chin.

Replies are listed 'Best First'.
Re: Comedy of StdErrs
by TomDLux (Vicar) on Aug 11, 2003 at 16:17 UTC

    Modules should detect data errors and return an appropriate error value, but it is not their place to shut down the program. The author of the main routine has to decide whether to shut down, use some default value, or handle the problem some other way.

    After all, if the program is running a nuclear reactor .....

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Comedy of StdErrs
by sgifford (Prior) on Aug 11, 2003 at 16:22 UTC

    I'm not sure I understand your question. The most common way to print a message and exit is with die:

    $date1 =~ /(\d\d)(\d\d)(\d\d)/ or die "Invalid date format"; $time1 =~ /(\d\d):(\d\d)/ or die "Invalid time format";

    Or are you talking about catching the error from gmtime? You're doing that properly, and I don't believe there's a better way to do it. When I have code like this, I will often wrap the entire thing in an eval, like this:

    eval { my $ref = shift; my $date1 = shift @$ref; my $time1 = shift @$ref; die unless $date1 =~ /(\d\d)(\d\d)(\d\d)/; my @date2 = ($1,$2,$3); die unless $time1 =~ /(\d\d):(\d\d)/; my @time2 = ($1,$2); my $t; $t = timegm(0,$time2[1],$time2[0],$date2[1],$date2[0]-1,$dat +e2[2]+100)}; }; print "-1\n" and exit if $@;
    It consolidates the error handling better.
Re: Comedy of StdErrs
by Mr. Muskrat (Canon) on Aug 11, 2003 at 16:24 UTC

    I couldn't get your code and Time::Local to invalidate Feb. 31, 2003 08:40 by passing a ref to ('023103','08:40') so I whipped this up. As you can see, I choose to use die statements as opposed to the print and exit combo.

    use Date::Calc qw(check_date check_time); our $DEBUG = 1; # set to 0 to disable print "woohoo!\n" if valid( ['033103','12:59'] ); # March 31, 2003 12: +59 valid print "w00t!\n" if valid( ['022803','13:00'] ); # February 28, 20 +03 13:00 valid print "Yippee!\n" if valid( ['023103','08:40'] ); # February 31, 2003 +8:40 invalid sub valid { my $ref = shift; my $date = shift @$ref; my $time = shift @$ref; die "Invalid date format $date" unless $date =~ /(\d\d)(\d\d)(\d\d +)/; my ($month, $day, $year) = ($1, $2, $3); $year += ($year <= 69) ? 2000 : 1900; die "Invalid date $date" unless (check_date($year,$month,$day)); die "Invalid time format $time" unless $time =~ /(\d\d):(\d\d)/; my ($hour, $minute) = ($1, $2); die "Invalid time $time" unless (check_time($hour,$min,$sec)); print "$date $time is valid.\n" if $DEBUG; return 1; }

    update: fixed $year update code

Re: Comedy of StdErrs
by sauoq (Abbot) on Aug 11, 2003 at 22:18 UTC
    Or is there another way to catch the error?

    As Time::Local calls croak() when a date is out of range, not really.

    You could, of course, write a wrapper that would always call timegm() in an eval for you.

    Or, if you aren't adverse to doing your own bounds checking or it isn't necessary, you could import the timegm_nocheck() function and use that instead.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Comedy of StdErrs
by BUU (Prior) on Aug 11, 2003 at 20:01 UTC
    I believe what you're getting at is "Best practices for catching and handling exceptions"? In which case you should try doing a super search for exceptions and study some of the nodes there. This is a subject that comes up once or twice a month as theres no real "right answer" (atleast not so far) so people keep debating it. Anyways, large numbers of other posts out there.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2020-05-30 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?