Perl error handling
During the operation of the program, there are always all kinds of errors, such as opening a non-existent file.
If an error stops during program operation, we need to use some detection methods to avoid errors and prevent the program from exiting.
Perl provides a multi-party approach to handling errors, and we'll cover them one by one.
The if statement
The if statement can determine the return value of the statement, as follows:
if(open(DATA, $file)){ ... }else{ die "Error: 无法打开文件 - $!"; }
Variable $in the program! A n error message was returned. We can also simplify the above code to the following:
open(DATA, $file) || die "Error: 无法打开文件 - $!";
The unless function
In contrast to if, the unless function is executed only when the expression returns false, as follows:
unless(chdir("/etc")){ die "Error: 无法打开目录 - $!"; }
The unless statement is useful when you want to set an error alert. Can I also short-write the above code as:
die "Error: 无法打开目录!: $!" unless(chdir("/etc"));
The above error message is output only if the directory switch is wrong.
The thyme operator
Here is a simple example of a thyme operator:
print(exists($hash{value}) ? '存在' : '不存在',"\n");
In the above example, we used a thyme operator to determine whether the value of the hash existed.
The instance contains two values of an expression in the form of: Expression ? Value One: Value Two.
The warn function
The warn function is used to trigger a warning message that is output to STDERR (standard output file) without additional action and is typically used to prompt the user:
chdir('/etc') or warn "无法切换目录";
The die function
The die function is similar to warn, but it performs an exit. The output of the error message is generally used:
chdir('/etc') or die "无法切换目录";
Carp module
In Perl scripts, the common way to report errors is to use the warn() or die() functions to report or produce errors. For the Carp module, however, it provides an additional level of control over the resulting messages, especially inside the module.
The standard Carp module provides an alternative to the warn() and die() functions, which provide more information and are more user-friendly in providing error location. When used in a module, the error message contains the module name and line number.
Carp function
The carp function outputs the program's trace information, similar to the warn function, which is typically sent to 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();
The above procedure is performed and the output is:
Error in module! at test.pl line 4
The cluck function
Cluck() is similar to warn() and provides backtracking from the stack 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;
Call the following program in the script:
use T; function();
The above procedure is performed and 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;
Call the following program in the script:
use T; function();
The above procedure is performed and the output is:
Error in module! at test.pl line 4
the confess function
confess() is similar to die(), but provides backtracking from the stack 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();
The above procedure is performed and the output is:
Error in module! at T.pm line 9 T::function() called at test.pl line 4