Perl Signal Processing Learning Simple Summary _perl

Source: Internet
Author: User
Tags error code eval

Common processing signals under Unix

Copy Code code as follows:

No Name Default Action Description
1 sighup Terminate process terminal line Hangup
2 SIGINT Terminate process interrupt Program
3 Sigquit Create core Image quit program
4 Sigill Create core image illegal instruction
5 Sigtrap Create core image trace trap
6 SIGABRT Create Core Image Abort Program (formerly Sigiot)
7 SIGEMT Create core image emulate instruction executed
8 SIGFPE Create core Image floating-point exception
9 SIGKILL Terminate process Kill program
Sigbus Create core Image Bus error
One SIGSEGV create core image Segmentation violation
Sigsys Create core Image non-existent system call invoked
Sigpipe terminate process write on a pipe with no reader
SIGALRM Terminate process Real-time timer expired
Sigterm terminate process software termination signal
Sigurg Discard signal urgent condition present on socket
SIGSTOP Stop Process Stop (cannot be caught or ignored)
SIGTSTP stop process stop signal generated from keyboard
Sigcont Discard signal continue after stop
SIGCHLD Discard signal child status has changed
Sigttin Stop process Background read attempted from Control terminal
Sigttou Stop process Background write attempted to control terminal
Sigio Discard signal I/O is possible on a descriptor (= = (2))
SIGXCPU Terminate process CPU time limit exceeded (")" (* Setrlimit (2))
Sigxfsz Terminate process file size limit exceeded (in the Setrlimit (2))
SIGVTALRM Terminate Process virtual time alarm (Setitimer (2))
SIGPROF Terminate Process profiling timer alarm (= Setitimer (2))
Sigwinch Discard signal Window size change
Siginfo Discard signal status request from keyboard
SIGUSR1 terminate process User defined signal 1
SIGUSR2 terminate process User defined signal 2

The principle of signal processing in Perl

Perl provides%sig this particular default hash. Calls need to use the system to keep the global hash array%sig, that is, to use the ' $SIG {signal name} ' to intercept the signal, which is equivalent to executing the address value of our own definition of a piece of code (a child function) in the Perl program ( Defines the signal response function, which is the result of intercepting this information.

Take a sigalrm example, which is the timeout process:

Copy Code code as follows:

my $timeout = 10;
eval {
The local $SIG {ALRM} = sub {die "alarm\n"}; # \ Required
Alarm $timeout; #如果到了 $timeout time to execute the upper sub.
Sleep 15;
Print "If timeout, this would not print";
Alarm 0; #恢复到默认的状态
};
if ($@) {
Die unless $@ eq "alarm\n"; #可能捕获的不是超时, it's another mistake, die.
print "timeout \ n";
}
else {
Print "Not timeout";
}

Here's a little bit of Perl's error-trapping mechanism,

Copy Code code as follows:

eval {
Open (FH, "domains.txt") or die "Can ' t open files,$!";
};

Catch exception
Copy Code code as follows:

if ($@) {#出现异常}
else{#无异常, print the contents of the file
while () {
...
}
Close FH;
}

If a program in the eval block has a syntax error, run-time error, or encounters a die statement, Eval returns UNDEF. The error code is saved in the $@.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.