?? ch13.htm
字號:
<PRE>
^C at test.pl line 22
</PRE>
</BLOCKQUOTE>
<P>
Of course, the filename and line number change to match the particulars
of whatever script happens to be running when Ctrl+C was pressed.
The <TT>^C</TT> notation refers to
the Ctrl+C key sequeNCe.
<H3><A NAME="ExampleHowtoHandleaSignal">
Example: How to Handle a Signal</A></H3>
<P>
You can cause Perl to ignore the Ctrl+C key sequeNCe by placing
the following line of code near the beginning of your program:
<BLOCKQUOTE>
<PRE>
$SIG{'INT'} = 'IGNORE';
</PRE>
</BLOCKQUOTE>
<P>
You can restore the default handler like this:
<BLOCKQUOTE>
<PRE>
$SIG{'INT'} = 'DEFAULT';
</PRE>
</BLOCKQUOTE>
<P>
If you need to ensure that files are closed, error messages are
written, or other cleanup chores are completed, you need to create
a custom <TT>INT</TT> handle fuNCtion.
For example:
<BLOCKQUOTE>
<PRE>
sub INT_handler {
# close all files.
# send error message to log file.
exit(0);
}
$SIG{'INT'} = 'INT_handler';
</PRE>
</BLOCKQUOTE>
<P>
If the Ctrl+C key sequeNCe is pressed anytime after the hash assignment
is made, the <TT>INT_handler</TT>
fuNCtion is called instead of the default handler.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
In theory, you could remove the <TT>exit()</TT> call from the signal handler fuNCtion, and the script should start executing from wherever it left off. However, this feature is not working on several platforms. If you want to test your platform, run the
following small program:
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>sub INT_handler {<BR>
print("Don't Interrupt!\n");<BR>
}<BR>
<BR>
$SIG{'INT'} = 'INT_handler';<BR>
for ($x = 0; $x < 10; $x++) {<BR>
print("$x\n");<BR>
sleep 1;<BR>
}</TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
You should be able to press Ctrl+C while the script is counting without forcing the script to end.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
The <TT>%SIG</TT> associative array
holds only entries you have created for your custom signal handler
fuNCtions. So, unfortunately, you can't find out which signals
are supported by looking at the array returned by <TT><I>keys</FONT></I><FONT FACE="Courier">(%SIG)</TT>.
<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you are running Perl on a UNIX machine, you can run the <TT>kill -l</TT> command. This command displays a list of possible signals.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
I looked directly into the perl.exe file supplied with my Perl
distribution to find out that the hip port of Perl for Win32 supports
the following signals:
<BLOCKQUOTE>
<TT>ABRT2</TT>-This signal means that
another process is trying to abort your process.
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>BREAK2</TT>-This signal indicates
that a Ctrl+Break key sequeNCe was pressed under Windows.
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>TERM2</TT>-This signal means that
another process is trying to terminate your process.
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>SEGV2</TT>-This signal indicates
that a segment violation has taken place.
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>FPE2</TT>-This signal catches
floating point exceptions.
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>ILL2</TT>-This signal indicates
that an illegal instruction has been attempted.
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>INT2</TT>-This signal indicates
that a Ctrl+C key sequeNCe was pressed under Windows.
</BLOCKQUOTE>
<P>
You can also use the <TT>%SIG</TT>
hash to trap a call to the <TT>warn()</TT>
and <TT>die()</TT> fuNCtions. This
comes in handy if you're working with someone else's code and
want to keep a log of whenever these fuNCtions are called. Rather
than finding every place the fuNCtions are used, you can define
a handler fuNCtion as in Listing 13.4.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Define a handler for the </I><TT><I>warn()</I></TT><I>
fuNCtion. The error message is passed to the handler as the first
element of the </I><TT><I>@_ array</I></TT><I>.
<BR>
Define a handler for the </I><TT><I>die()</I></TT><I>
fuNCtion.<BR>
Define the </I><TT><I>sendToLogfile()</I></TT><I>
utility fuNCtion.<BR>
Start the signal catching by creating two entries in the </I><TT><I>%SIG</I></TT><I>
hash.<BR>
Invoke the </I><TT><I>warn()</I></TT><I>
and </I><TT><I>die()</I></TT><I> fuNCtions.</I>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<B>Listing 13.4 13LST04.PL-How to Define Signal Handler
FuNCtions for the </B><TT><I><B><FONT FACE="Courier">warn()</FONT></B></I></TT><B>
and </B><TT><I><B><FONT FACE="Courier">die() </FONT></B></I></TT><B>FuNCtions
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
sub WARN_handler {
my($signal) = @_;
sendToLogfile("WARN: $signal");
}
sub DIE_handler {
my($signal) = @_;
sendToLogfile("DIE: $signal");
}
sub sendToLogfile {
my(@array) = @_;
open(LOGFILE, ">>program.log");
print LOGFILE (@array);
close(LOGFILE);
}
$SIG{__WARN__} = 'WARN_handler';
$SIG{__DIE__} = 'DIE_handler';
chdir('/printer') or warn($!);
chdir('/printer') or die($!);
</PRE>
</BLOCKQUOTE>
<HR>
<P>
When this program is done executing, the <TT>PROGRAM.LOG</TT>
file contains these lines:
<BLOCKQUOTE>
<PRE>
WARN: No such file or directory at 13lst02.pl line 22.
DIE: No such file or directory at 13lst02.pl line 23.
</PRE>
</BLOCKQUOTE>
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>
Summary</FONT></A></H2>
<P>
Your program's capability to handle error conditions that may
arise will determine, to a certain extent, how usable your program
is. If a user of your program finds that it stops working with
no error messages and, therefore, no way to solve whatever problem
has arisen, then your program won't be used for long.
<P>
Displaying error messages is also valuable during the programming
and debugging stage. If you mistakenly type a directory name,
it may take you an hour to look through the script and find the
problem. Handling the <TT>No such directory</TT>
error correctly in the first place will tell you what the problem
is and which line of the script has the problem.
<P>
In this chapter, you saw that checking for errors usually means
looking at the return value of the fuNCtions that are called.
Some fuNCtions set the <TT>errno</TT>
variable while others simply return true or false. While the <TT>errno</TT>
variable does have a core set of values that are system independent,
it also has system-dependent values. Listing 13.1 showed you how
to display the error values applicable to your system.
<P>
Next, you read about the <TT>or</TT>
logical operator. This operator evaluates only the right operand
if the left is false. Therefore, it is useful when testing for
unsuccessful fuNCtions that return false upon failure.
<P>
The <TT>die()</TT> and <TT>warn()</TT>
fuNCtions are both used to display an error message. In addition,
the <TT>die()</TT> fuNCtion causes
the script to end.
<P>
Then, the <TT>eval()</TT> fuNCtion
was covered. It is used to execute Perl code in a protected environment
so that fatal errors will not end the script. Any error messages
that do arise will be placed into the <TT>$@</TT>
special variable. All variable value changes and fuNCtion definitions
affect the main program.
<P>
Lastly, the signals were covered. Signals are messages sent to
a process by the operating system. There is a wide range of signals,
and they differ depending on which operating system you are using.
The <TT>%SIG</TT> associative array
is used to set up your own signal handling fuNCtion.
<P>
The next chapter discusses object orientation. You learn the definition
of an object, how to create one, and how to derive new objects
from existing objects.
<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#FF0000>
Review Questions</FONT></A></H2>
<P>
Answers to Review Questions are in Appendix A.
<OL>
<LI>Why is it important to check for errors?
<LI>How is the <TT>die()</TT> fuNCtion
different from the <TT>warn()</TT>
fuNCtion?
<LI>What is the meaning of the <TT>$!</TT>
special variable?
<LI>What does the <TT>eval()</TT>
fuNCtion do?
<LI>What is a signal?
<LI>What will the statement <TT>$SIG{'ABRT'}
= 'IGNORE'</TT> do?
<LI>Which signal is used to trap floating point exceptions?
</OL>
<H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>
Review Exercises</FONT></A></H2>
<OL>
<LI>Write a program that opens a file and uses the <TT>die()</TT>
fuNCtion if an error occurs.
<LI>Write a program that uses the <TT>warn()</TT>
fuNCtion if an existing file will be overwritten by an <TT>open()</TT>
statement.
<LI>List three situations where the <TT>warn()</TT>
fuNCtion could be used.
<LI>List three situations where the <TT>die()</TT>
fuNCtion could be used.
<LI>Modify the interactive Perl interpreter to print a version
number when the <TT>version</TT> custom
command is used.
<LI>Modify the interactive Perl interpreter to save all commands
entered into a log file. Add a timestamp to each log entry.
</OL>
<HR>
<CENTER><P><A HREF="ch12.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch12.htm"><IMG SRC="pc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="index-1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/index-1.htm"><IMG SRC="hb.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="ch14.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch14.htm"><IMG SRC="nc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<HR WIDTH="100%"></P></CENTER>
</BODY>
</HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -