At 02:37 PM 11/20/01 +0100, Teggy P Veerapen wrote:
>Hello,
>
>     I'm new to perl but not to programming, and I have been searching
>(without any success up to now) how perl handles errors. Does perl offer a
>system of error management ?

Kind of.  You sound like you're looking for something like the Java 
interface where everything advertises which exception classes it can 
throw.  Perl doesn't do that (although I and others proposed something 
close to it for Perl 6).  But it isn't far off; after all, you have to read 
the documentation for any function that you call anyway to see what they'll do.

The vast majority of Perl builtins and third-party module functions will 
return some value to signify error.  You have to check the documentation to 
find out what.  Some will signify error via another variable which you have 
to check.  A few will throw exceptions instead; in Perl this is done with 
"die", and they are caught with "eval".

>Where can I find information (faq, URL or any
>other ...) about perl error management ?

perldoc -f die
perldoc -f eval

That may not be enough for you though.  Shameless plug though it may be, I 
expended a chapter (#9) on this topic in my book (URL below).

>     For example I'm using the following Time::Local call timelocal. This
>function takes as input 6 numeric fields and I would like to call this
>function. If this function returns an error (because parameters are not
>numeric ...), then I should be able to sort of get this error and do some
>processing of mine.

This is a good question.  The Time::Local documentation doesn't say.  It 
also says that by default it does range checking on arguments but doesn't 
say what happens if they fail.  You have to try it and see (or read the 
source):

% perl -Mstrict -MTime::Local -wle 'print timelocal(0,0,0,35,0,99)'
Day '35' out of range 1..31 at -e line 1

Ah; it dies.  And with non-numeric data:

% perl -Mstrict -MTime::Local -wle 'print timelocal(0,0,0,"foo",0,99)'
Argument "foo" isn't numeric in numeric gt (>) at 
/afs/jpl/rep/f/fil/uai/perl/lib/5.6.1/Time/Local.pm line 90.
Day 'foo' out of range 1..31 at -e line 1

So it still died (because "foo" numericizes to zero), but you got a warning 
first with -w or use warnings.

A block eval could catch the die().  As for ensuring that you're passing 
numeric data, well, you have to decide what you want to happen.  Perhaps 
you'd like to turn all warn() calls into die()s via this statement:

local $SIG{__WARN__} = sub {die $_[0]};
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to