Perl Error handling

  • You can identify and trap an error in a number of different ways. 
  • Its very easy to trap and handle the errors in Perl. Here are few methods which can be used.
1) if Statement
Based on return statement you can handle the errors using if loop
1
2
3
4
5
6
if (open(DATA,$file)){
   ...
}
else{
   die "Error: Couldn't open the file $!";
}

2) Unless Function
  • The unless function is the logical opposite to if: statements can completely bypass the success status and only be executed if the expression returns false.
  • The unless statement is best used when you want to raise an error or alternative only if the expression fails.
1
2
3
4
unless(chdir("/etc"))
{
   die "Error: Can't change directory!: $!";
}

3) Try::Tiny Module
This is a external module. You can use try and catch to expect and handle exceptional conditions, avoiding quirks in Perl and common mistakes.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use Try::Tiny; 
$a = 12;
$b = 0;

try {
        print $a/$b;
        
} catch {
        warn "caught arithmatic exception:";
}finally { 
       #Some code..
};
Output: caught arithmatic exception:

4) 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.

1
2
3
4
5
use strict;    
use warnings;    
my $filename = 'sssit/javatpoint/file1.txt';    
open(my $fh, '>', $filename) or warn "Can't open file";     
print "done\n";  
Output:
Can't open file at delete_1.pl line 4.
done

5) 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.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
use strict;  
use warnings;  

#The open() function will only open your file as usual. The die() function throws an exception and just exit the script.
#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.
open(my $fh, '>', 'sssit/javatpoint/file1.txt') or die;  
print $fh "Example to show Error Handling\n";  
close $fh;  
print "done\n";  
Output: Died at delete_1.pl line 7.

6) 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.

1
2
3
4
5
6
use strict;    
use warnings;  
use Carp;  
my $filename = 'sssit/javatpoint/file1.txt';    
open(my $fh, '>', $filename) or confess($!);     
print "done\n";  
Output: Can't open file at delete_1.pl line 4.
done

7) Eval
If we wrap a piece of code in an eval block, the eval will capture any exception that might happen within that block, and it will assign the error message to the $@ variable of Perl.
1
2
3
4
5
6
eval {
     # code that might throw exception
};
if ($@) {
    # report the exception and do something about it
}


<-- Previous || Next -->

1 comment: