Perl Error Handling

On execution of a program, errors also run along with the program. If these errors will not be handled properly, then the program may not run smoothly.

Error handling let us handle these errors by taking appropriate action and run program smoothly. Various types of errors can be handled by error handling.

You need to catch the error and run your program smoothly by rectifying the error. Perl provides a number of ways to do so which are illustrated below.

Without Die function

The die() function gives you a proper error message. It immediately terminates the script on encountering an error. If you won't use die() function in your script, your script will keep on running.

Example
snippet
use strict;
use warnings;
open(my $fh, '>', 'sssit/rookienerd/file1.txt');
print $fh "Example to show Error Handling\n";
close $fh;
print "done\n";
Output
Print() on closed filehandle $fh at script.pl done

Look at the above output, the script keeps on running on encountering error printing 'done'.

Open or Die

The open() function will only open your file as usual. The die() function throws an exception and just exit the script.

In 'open or die' function, on left side we have open() function. On right side we have die() function.

If open() function returns true, then script goes on next line and die() function doesn't execute.

If open() function returns false, then script goes on die() function which throws an exception and exit the script.

Example

Here, in this example we have given the wrong path of the file due to which die() function will execute and exit the script.

snippet
use strict;
use warnings;
open(my $fh, '>', 'sssit/rookienerd/file1.txt') or die;
print $fh "Example to show Error Handling\n";
close $fh;
print "done\n";
Output
Died at script.pl

In the output, as we use die() function script exit on encountering error. Hence, 'done' is not printed.

Adding explanation in Die

If you want to add some explanation about the error, you can add it in die() function. If your script dies, this explanation will print as the error message.

Example
snippet
use strict;
use warnings;
open(my $fh, '>', 'sssit/rookienerd/report.txt')
  or die "Could not open file due to 'sssit/rookienerd/report.txt'";
close $fh;
print "done\n";
Output
Could not open file due to 'sssit/rookienerd/report.txt'at script.pl

Look at the above output, we got an explanation about the error in our script.

Error Reporting using $!

The $! Variable is a built in variable in Perl language. By adding explanation in die() function, we know the error message but we still don't know the reason behind it. To know the exact reason of error use $! variable. It will print the message told by operating system about the file.

Example
snippet
use strict;
use warnings;
my $filename = 'sssit/rookienerd/file1.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!"; 
close $fh;
print "done\n";
Output
Could not open file 'sssit/rookienerd/file1.txt' No such file or directory

warn Function

A warn function gives a warning message but does not exit the script. The script will keep on running. Hence, it is useful when you only want to print the warning message and proceed for the rest of the program.

Example
snippet
use strict;  
use warnings;  
my $filename = 'sssit/rookienerd/file1.txt';  
open(my $fh, '>', $filename) or warn "Can't open file";   
print "done\n";
Output
Can't open file at hw.pl at line 4. done

Look at the above output, we have printed 'done' to show that execution continues even after printing warning message.

Error Reporting using confess Function

The modern approach for error handling is to use Carp standard library. The confess() function is used within Carp library. We have passed $! as its argument.

snippet
use strict;  
use warnings;
use Carp;
my $filename = 'sssit/rookienerd/file1.txt';  
open(my $fh, '>', $filename) or confess($!);   
print "done\n";
Output
No such file or directory. done

eval Function

An eval() function is a built-in function in Perl which is used to detect normal fatal error. The eval() function is supplied with a code block instead of passing into string.

If there are syntax errors, eval block will fail. But if a runtime error occurs, the script carries on running.

In the following program,there is no syntax error.

use strict; use warnings; my $result = eval { my $x = 10; my $y = 0; my $result2 = $x/$y; print "$result2"; }; print "Script is still running!\n"; unless($result) { print $@; }

Output
Script is still running! Illegal division by zero

Look at the above output, script keeps on running as their is no syntax error.

Difference between die() and confess()

The die() function is used when script short comprising of ten lines. The die() function can also be used without $!.

The confess() function is used inside carp package. For a larger script, it is better to use confess function.

Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +