5.37. Perl error handling

发布时间 : 2023-10-20 23:00:04 UTC      

Page Views: 33 views

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.

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 - $!";
}

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 - $!";

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"));

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

5.37.3. 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.

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 #

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.

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;

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

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;

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

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;

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

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;

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
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.