Perl error handling


Release date:2023-10-19 Update date:2023-10-21 Editor:admin View counts:373

Label:

Perl error handling

In the process of running the program, you will always encounter all kinds of errors, such as opening a file that does not exist.

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

Perl provides multiple methods to deal with errors, and then we will introduce them one by one.

If statement

if statement can determine the return value of the statement. An exampleis as follows:

if(open(DATA, $file)){
   ...
}else{
   die "Error: could not open file - $!";
}

Variables in the program $! an error message was returned. We can also simplify the above code to the following code:

open(DATA, $file) || die "Error: could not open file - $!";

Unless function

unless Function vs. if instead, only if the expression returns false will only be executed when, as shown below:

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

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

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

The above error message will only be output in the case of directory switching error.

Ternary operator

The following is a simple example of a ternary operator:

print(exists($hash{value}) ? 'exist' : 'absent',"\n");

In the above example, we use the ternary operator to determine whether the hash value exists.

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

Warn function

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

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

Die function

die function is similar to warn , but it performs an exit Generally used as the output of 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 errors. And for Carp module, which provides an additional level of control over the messages generated, especially within the module.

Standard Carp the module provides warn() and die() functions,which provide more information and are more friendly in providing error location. When used in a module, the error message contains the module name and line number.

Carp function

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

package T;

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

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

Call the following program in the script:

use T;
function();

Execute the above program, and the output is as follows:

Error in module! at test.pl line 4

Cluck function

cluck() and warn() are the same, stack backtracking from the place where the error occurred is provided.

package T;

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

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

Call the following program in the script:

use T;
function();

Execute the above program, and the output is as follows:

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

Croak function

croak() and die() are the same, you can end the script.

package T;

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

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

Call the following program in the script:

use T;
function();

Execute the above program, and the output is as follows:

Error in module! at test.pl line 4

Confess function

confess() and die() are the same, but provides stack backtracking from the place where the error occurred.

package T;

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

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

Call the following program in the script:

use T;
function();

Execute the above program, and the output is as follows:

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

Powered by TorCMS (https://github.com/bukun/TorCMS).