Perl error handling
When the program is running, you will always encounter various errors, such as opening a non-existent file.
If an error occurs during the running of the program, it will stop. We need to use some detection methods to avoid errors and prevent the program from exiting.
Perl provides multiple methods for handling errors, and we will introduce them one by one next.
if statement
if statement You can determine the return value of the statement. The example is as follows:
if(open(DATA, $file)){ ... }else{ die "Error: 无法打开文件 - $!"; }
The variable $! in the program returned an error information. We can also simplify the above code to the following code:
open(DATA, $file) || die "Error: 无法打开文件 - $!";
unless function
unless The function is the opposite of if, it only returns false when the expression Executed as follows:
unless(chdir("/etc")){ die "Error: 无法打开目录 - $!"; }
unless statement is very useful when you want to set an error reminder. We can also abbreviate the above code as:
die "Error: 无法打开目录!: $!" unless(chdir("/etc"));
The above error message will only be output when the directory switching error occurs.
Ternary operator
The following is a simple example of the ternary operator:
print(exists($hash{value}) ? '存在' : '不存在',"\n");
In the above example, we used the ternary operator to determine the hash whether the value exists.
The example contains an expression with two values, the format is: Expression ? Value one: Value two.
warn function
warn function is used to trigger a warning message. There will be no other operations. It is output to STDERR (standard output file). It is usually used to prompt the user:
chdir('/etc') or warn "无法切换目录";
die function
die function is similar to warn, but it will execute exit. Generally used to output error messages:
chdir('/etc') or die "无法切换目录";
Carp module
In Perl scripts, the common way to report errors is to use the warn() or die() function to report or generate mistake. In the case of the Carp module, it provides an additional level of control over the messages produced, especially within the module.
The standard Carp module provides alternatives to the warn() and die() functions that are more informative and friendlier in providing error location. When used within a module, the module name and line number are included in the error message.
carp function
The carp function can output the tracking information of the program, similar to the warn function, and usually sends the information 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();
Execute the above program, the output result is:
Error in module! at test.pl line 4
cluck Function
cluck() is similar to warn(), providing a stack traceback 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;
Call the following program in the script:
use T; function();
Execute the above program, the output result is:
Error in module! at T.pm line 9 T::function() called at test.pl line 4
croak function
croak() and die() Likewise, 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, the output result is:
Error in module! at test.pl line 4
confess function
confess() and die() Similar, 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;
Call the following program in the script:
use T; function();
Execute the above program, the output result is:
Error in module! at T.pm line 9 T::function() called at test.pl line 4