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.
Variables in the program The above error message will only be output in the case of directory switching error. The following is a simple example of a ternary operator: 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 In Standard Call the following program in the script: Execute the above program, and the output is as follows: Call the following program in the script: Execute the above program, and the output is as follows: Call the following program in the script: Execute the above program, and the output is as follows: Call the following program in the script: Execute the above program, and the output is as follows:
Perl
provides multiple methods to deal with errors, and then we will introduce them one by one. 5.37.1. 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 - $!";
}
$!
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 - $!";
5.37.2. 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"));
5.37.3. Ternary operator #
print(exists($hash{value}) ? 'exist' : 'absent',"\n");
?
value one
:
value two. 5.37.4. 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";
5.37.5. 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";
5.37.6. Carp module #
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.
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. 5.37.7. 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;
use T;
function();
Error in module! at test.pl line 4
5.37.8. 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;
use T;
function();
Error in module! at T.pm line 9
T::function() called at test.pl line 4
5.37.9. 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;
use T;
function();
Error in module! at test.pl line 4
5.37.10. 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;
use T;
function();
Error in module! at T.pm line 9
T::function() called at test.pl line 4