home PYTHONJAVA
 

Perl error handling

When the program is running, you will always encounter a variety of errors, such as opening a file that does not exist.

If an error occurs during the running of the program, we will need to use some detection methods to avoid errors and prevent the program from exiting.

Perl provides a number of ways to handle error sending, and we'll cover them one by one.


if statement

if statement can determine the return value of a statement, as an example:

if(open(DATA, $ File)){
   ...
}else{
   die "Error: Unable to open file - $!";
}

The variable $! in the program returned an error message. We can also simplify the above code into the following code:

.
open(DATA, $file) || die "Error: Unable to open file - $!";

unless function

The

unless function is the opposite of if and will only be executed if the expression returns false as follows:

unless(chdir("/etc")){
   die "Error: Unable to open directory - $!";
}
The

unless statement is very useful when you want to set error alerts. We can also abbreviate the above code as:

die "Error: Unable Open the directory!: $!" unless(< Span class="pln">chdir("/etc") );

The above error message will only be output if the directory switch error occurs.


Ternary operator

The following is a simple example of a ternary operator:

print(exists($hash{ Value}) ? 'Existence' : 'Not present',"\n");

In the above example we used the ternary operator to determine if the hash value exists.

The instance contains two values ​​for an expression in the format: expression ? value one : value two.


warn function

The

warn function is used to trigger a warning message, there is no other operation, output to STDERR (standard output file), usually used to prompt the user:

chdir('/etc ') or warn "Unable to switch directories";

die function

The

die function is similar to warn, but it performs an exit. Generally used as output for error messages:

chdir('/etc ') or die "Unable to switch directories";

Carp module

In Perl scripts, a common way to report errors is to use the warn() or die() function to report or generate an error. For the Carp module, it provides an extra level of control over the generated messages, especially inside the module.

The standard Carp module provides an alternative to the warn() and die() functions, which provide more information and provide more friendly information for error location. When used in a module, the error message contains the module name and line number.

carp function

The carp function can output trace information for the program, similar to the warn function, which is usually sent to STDERR:

package T;

require Exporter ;
@ISA = qw/Exporter/;
@EXPORT = qw/function/;
use Carp ;

sub function {
   Carp "Error in module!";
}
1;

In the script call the following program:

use T;
function();

Execute the above program, the output is:

Error in module! at test.pl line 4

cluck function

cluck() is similar to warn() in that it provides trace backtracking from where the error occurred.

package T;

require Exporter ;
@ISA = qw/Exporter/;
@EXPORT = qw/function/;
use Carp Qw(cluck);

sub function {
   Cluck "Error in module!";
}
1;

In the script call the following program:

use T;
function();

Execute the above program, the output is:

Error in module! at T.pm line 9
    T::function() called at test.pl line 4

croak function

croak() Like die(), you can end the script.

package T;

require Exporter ;
@ISA = qw/Exporter/;
@EXPORT = qw/function/;
use Carp ;

sub function {
   Croak "Error in module!";
}
1;

In the script call the following program:

use T;
function();

Execute the above program, the output is:

Error in module! at test.pl line 4

confess function

confess() is similar to die() but provides a stack traceback from where the error occurred.

package T;

require Exporter ;
@ISA = qw/Exporter/;
@EXPORT = qw/function/;
use Carp ;

sub function {
   Confess "Error in module!";
}
1;

In the script call the following program:

use T;
function();

Execute the above program, the output is:

Error in module! at T.pm line 9
    T::function() called at test.pl line 4





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.