Errors and Trapping Them – Perl

In this post I will review how to trap errors in Perl. It is not related to find and debug programs (I will post another page about that) but it will just check how to handle errors in our programs. For example, when you open a file, what happens if the file doesn’t exist? Or if it’s a new file, what do you do if the operating system doesn’t let you create a new file? Why we need to take care of this kinds of errors? The reason is quite simple – We need to stop our script from doing something we don’t expect it to do – I don’t think you still want the program to continue if your program fails to create a file which will be used by the program later.


Below is the principles of Errors handing in Perl,

What to Check
There are statements and operations that you should always check, whether you are interested in the return value or not, if only to prevent your script from doing something it shouldn’t:
 The open, close, and related statements that provide a conduit to the outside world (including socket and tie) and external database connectivity
 Reading from or writing to a file or socket handle other than STDIN, STDOUT,or STDERR
 Reading from or writing to STDIN, STDOUT, or STDERR if they have been reassigned or redirected within the script
 Anything that makes changes to the operating system or file system, including calls like unlink
 Anything that changes the environment in which you are operating (for example, chdir and chroot) or modifies the %ENV hash
 Any system call not already covered—system calls always return their status, so use it!
 Anything that relies on input or information supplied the user—don’t automatically assume that users know what they are doing!
 Any calls to an external program, either through open, the qx operator, or system
 The object type when a reference that points to an internal object is dynamically generated—particularly vital for code references

Beyond the preceding list, it comes down to a case of how the operation will affect the execution of your script. If the operation will affect the following statements, then it needs to be traced and probably trapped to prevent it from it having a knock on effect.
As a good example, using zero as a division value will raise an error, so it’s a good idea to check the value beforehand.


Warn & Die Function

The warn function just raises a warning message printed to STDERR. However a die function works just like warn, except that it also calls exit.


Error Mesages and Numbers

When Perl reports an error, it will store the error number or error string for the last system error that occured in the special $! variable.

#! /usr/local/bin/perl -w

#Error Message
open (DATA, “abc.txt”) or warn “Can’t open the file: $! \n”;
#Error Number
open (DATA, “abc.txt”) or warn “Can’t open the file: “, 0+$!, “\n”;


(luhuang) public_html- ./
Can’t open the file: No such file or directory
Can’t open the file: 2


Directives and Tokens

The special tokens __FILE__ and __LINE__ contain the currently executing line number and the file in which the line appears.

open (DATA, “abc.txt”) or warn “Can’t open the file: “, __LINE__, __FILE__ ,”\n”;


Adding Error Checking to the Scripts.

Error Checking Methods,

Method 1:

Using if. – The if statement is the obvious choice when you need to check the return value from a statement; for example:

if (open(DATA,$file) {


else {

die “It fails to open file: $!”;


Method 2:

Using unless, it is the logical opposite to if.

Method 3:

Using Short-Circuit Logic

open(DATA,”$file”) or die “failed to open $file: $! \n”;

Method 4:

Using the Conditional Operator:

mkdir “$newdir” ? print “Created newdir \n” : die “Can’t create newdir: ?!”;