Perl Error Handling - Perl Scripting

What is Error handling in Perl? what is its function?

Error handling is an important part in programming. Whenever you find an error, then the user needs to handle the situation properly and correctly in executing the program. The execution and the errors always go hand-in-hand. If you want to open a file that do not exist, at this point if the user did not handle this situation properly then the program is considered to be of bad quality.
The program stops if an error occurs. So, in this case a proper error handling is used to handle various types of errors, which may occur during a program execution and should take necessary action instead of stopping the program completely.
You can identify the error and trap in a number of different ways. It is very easy to find out the errors in Perl and then handling them properly. Here are few methods which can be used.

The if statement

The if statement is the obvious choice when you need to check the return value from a statement; for example −
Here variable $! returns the actual error message. So, in that case an alternative choice has to be taken, we can reduce the statement to one line in situations where it makes sense to do so; for example −

The unless Function

The unless function is the logical one opposite to if: statements can completely bypass the success status and only be executed if the expression returns false. For example −
The unless statement is best used when you want to raise an error or alternative only if the expression returns false. The statement also makes sense when used in a single-line statement −
Here we die only if the chdir operation fails, and it reads nicely.

The ternary Operator

For very short tests, you can use the conditional operator ?:
The effect is the same as using an if or unless statement. The conditional operator is best used when you want to quickly return one of the two values within an expression or statement.

The warn Function

The warn function raises a warning, a message will be printed as STDERR, but no action has taken. So, it is more useful if you just want to print a warning for the user and proceed with rest of the operation −

The die Function

The die function works just like warn, except that it also calls exit. Within a normal script, this function helps in immediate terminating at the time of execution. You should use this function in case it is of waste to move further if there is an error in the program −

Errors within Modules

There are two different situations we should be able to handle −
  • Reporting an error in a module that indicates the module's filename and line number - this is useful when you debug a module, or when you specifically want to raise a module-related, rather than script-related, error.
  • Reporting an error within a module that indicates the caller's information so that you can debug the line within the script where the error is caused. Errors raised in this module are useful to the end-user, because they highlight the error in relation to the calling script's origination line.
The warn and die functions work slightly differently than you would expect when called from within a module. For example, the simple module -
It will produce the following result −
This is more or less what you might be expected, but not necessarily what you want. From a module programmer's point of view, the information is very useful because it helps to point to a bug within the module itself. In other case for an end-user, the information provided is fairly useless, and for the hardened programmer, it is completely pointless.
The solution for such problems is the Carp module, which provides a simplified method for reporting errors within modules that return information about the calling script. The Carp module provides four functions: carp, cluck, croak, and confess. These functions are discussed below.

The carp Function

The carp function is the basic equivalent of warn and prints the message to STDERR without actually exiting the script and printing the script name.
When called from a script like below −
It will produce the following result −

The cluck Function

The cluck function is a sort of supercharged carp, it follows the same basic principle but also prints a stack trace of all the modules that led to the function being called, including the information on the original script.
When called from a script like below −
It will produce the following result −

The croak Function

The croak function is equivalent to die, except that it reports the caller one level up. Like die, this function also exits the script after getting the error to STDERR −
When called from a script like below −
It will produce the following result −
As with carp, the same basic rules apply regarding the including of line and file information according to the warn and die functions.

The confess Function

The confess function is like cluck; it calls die and then prints a stack trace all the way up to the origination script.
When called from a script like below −
It will produce the following result −

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Perl Scripting Topics