Home >> perlexamples >>
handling Errors in perl
Use the standard means of handling Perl errors by using the Perl eval statement to analyze errors. Use the following example:
eval{open(FILE, $file);};
if ($@)
{
print "Error opening file. Error: $@\n";
}
else
{
# continue without error ...
}
die
Outside an eval(), prints the value of LIST to STDERR and exits with the current value of $! (errno). If $! is 0, exits with the value of ($? >> 8) (backtick `command` status). If ($? >> 8) is 0, exits with 255. Inside an eval(), the error message is stuffed into $@ and the eval() is terminated with the undefined value. This makes die() the way to raise an exception.
Equivalent examples:
die "Can't cd to /etc/passwd: $!\n" unless chdir '/etc/passwd';
chdir '/etc/passwd' or die "Can't cd to /etc/passwd: $!\n"
Perl functions
|
How to include module in perl? number of matches in regex Perl Data Dumper perl: How to parse config file
Download perl
download stable version of perl
Perl 5.10.1
|
|