Latest web development tutorials

Perl error handling

The program is running, always encounter a variety of errors, such as opening a file that does not exist.

The program is running will stop if an error occurs, we need to use some methods to avoid detection errors, thus preventing the program exits.

Perl provides a multi-processing error made in the method, then we introduced one by one.


if statement

if the statement can determine the return value of the statement, the examples are as follows:

if(open(DATA, $file)){
   ...
}else{
   die "Error: 无法打开文件 - $!";
}

Program variable $! Returned an error message. We can also simplify the code above to the following code:

open(DATA, $file) || die "Error: 无法打开文件 - $!";

unless function

unless function and if the contrary, only when the expression returns false will be implemented as follows:

unless(chdir("/etc")){
   die "Error: 无法打开目录 - $!";
}

unless the statement when you want to set a reminder error is very useful.What I can also be abbreviated as the code above:

die "Error: 无法打开目录!: $!" unless(chdir("/etc"));

The above error message only when the circumstances will change directory error output.


Ternary operator

The following is a simple example of a ternary operator:

print(exists($hash{value}) ? '存在' : '不存在',"\n");

The above examples we use the ternary operator to determine the hash value exists.

Examples included an expression of two values, the formatis:? A value of the expression: Value II.


warn function

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

chdir('/etc') or warn "无法切换目录";

die function

die function is similar to warn, but it will be an exit. Usually with loud noise output error messages:

chdir('/etc') or die "无法切换目录";

Carp module

In Perl script, commonly used method is to use the error report warn () or die () function to generate reports or error. For the Carp module, which can generate messages to provide additional levels of control, especially inside the module.

Standard Carp module provides a warn () and die () function is an alternative, they provide more information in the provision of wrong positioning, and more friendly. When used in a module, the error message contains the module name and line number.

carp function

carp function can output tracking information about the program, similar to warn function will normally send the information to STDERR:

package T;

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

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

In the script calls the following procedure:

use T;
function();

The above program, the output is:

Error in module! at test.pl line 4

cluck function

cluck () and warn () is similar, provided the error at the stack back trace.

package T;

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

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

In the script calls the following procedure:

use T;
function();

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 () and die (), as can finish script.

package T;

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

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

In the script calls the following procedure:

use T;
function();

The above program, the output is:

Error in module! at test.pl line 4

confess function

confess () and die () is similar, but offers from the error of the stack back trace.

package T;

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

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

In the script calls the following procedure:

use T;
function();

The above program, the output is:

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