📄 ch15.htm
字号:
</TD><TD WIDTH=451><TT>$INPUT_RECORD_SEPARATOR</TT> or <TT>$RS</TT>
</TD></TR>
<TR><TD COLSPAN=2 WIDTH=590><B>Output</B></TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$|</TT></CENTER>
</TD><TD WIDTH=451><TT>$OUTPUT_AUTOFLUSH</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$,</TT></CENTER>
</TD><TD WIDTH=451><TT>$OUTPUT_FIELD_SEPARATOR</TT> or <TT>$OFS</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$\</TT></CENTER>
</TD><TD WIDTH=451><TT>$OUTPUT_RECORD_SEPARATOR</TT> or <TT>$ORS</TT>
</TD></TR>
<TR><TD COLSPAN=2 WIDTH=590><B>Formats</B></TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$%</TT></CENTER>
</TD><TD WIDTH=451><TT>$FORMAT_PAGE_NUMBER</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$=</TT></CENTER>
</TD><TD WIDTH=451><TT>$FORMAT_LINES_PER_PAGE</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$_</TT></CENTER>
</TD><TD WIDTH=451><TT>$FORMAT_LINES_LEFT</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$~</TT></CENTER>
</TD><TD WIDTH=451><TT>$FORMAT_NAME</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^</TT></CENTER>
</TD><TD WIDTH=451><TT>$FORMAT_TOP_NAME</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$:</TT></CENTER>
</TD><TD WIDTH=451><TT>$FORMAT_LINE_BREAK_CHARACTERS</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^L</TT></CENTER>
</TD><TD WIDTH=451><TT>$FORMAT_FORMFEED</TT>
</TD></TR>
<TR><TD COLSPAN=2 WIDTH=590><B>Error Status</B></TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$?</TT></CENTER>
</TD><TD WIDTH=451><TT>$CHILD_ERROR</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$!</TT></CENTER>
</TD><TD WIDTH=451><TT>$OS_ERROR</TT> or <TT>$ERRNO</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$@</TT></CENTER>
</TD><TD WIDTH=451><TT>$EVAL_ERROR</TT>
</TD></TR>
<TR><TD COLSPAN=2 WIDTH=590><B>Process Information</B></TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$$</TT></CENTER>
</TD><TD WIDTH=451><TT>$PROCESS_ID</TT> or <TT>$PID</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$<</TT></CENTER>
</TD><TD WIDTH=451><TT>$REAL_USER_ID</TT> or <TT>$UID</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$></TT></CENTER>
</TD><TD WIDTH=451><TT>$EFFECTIVE_USER_ID</TT> or <TT>$EUID</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$(</TT></CENTER>
</TD><TD WIDTH=451><TT>$REAL_GROUP_ID</TT> or <TT>$GID</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$)</TT></CENTER>
</TD><TD WIDTH=451><TT>$EFFECTIVE_GROUP_ID</TT> or <TT>$EGID</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$0</TT></CENTER>
</TD><TD WIDTH=451><TT>$PROGRAM_NAME</TT>
</TD></TR>
<TR><TD COLSPAN=2 WIDTH=590><B>Internal Variables</B></TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$]</TT></CENTER>
</TD><TD WIDTH=451><TT>$PERL_VERSION</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^A</TT></CENTER>
</TD><TD WIDTH=451><TT>$AccUMULATOR</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^D</TT></CENTER>
</TD><TD WIDTH=451><TT>$DEBUGGING</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^F</TT></CENTER>
</TD><TD WIDTH=451><TT>$SYSTEM_FD_MAX</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^I</TT></CENTER>
</TD><TD WIDTH=451><TT>$INPLACE_EDIT</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^P</TT></CENTER>
</TD><TD WIDTH=451><TT>$PERLDB</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^T</TT></CENTER>
</TD><TD WIDTH=451><TT>$BASETIME</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^W</TT></CENTER>
</TD><TD WIDTH=451><TT>$WARNING</TT>
</TD></TR>
<TR><TD WIDTH=139><CENTER><TT>$^X</TT></CENTER>
</TD><TD WIDTH=451><TT>$EXECUTABLE_NAME</TT>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Listing 15.8 shows a program that uses one of the English variables
to access information about a matched string.
<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>Load the </I><TT><I>English</I></TT><I>
module.<BR>
Invoke the strict pragma.<BR>
Initialize the search space and pattern variables.<BR>
Perform a matching operation to find the pattern <BR>
in the </I><TT><I>$searchSpace</I></TT><I>
variable.<BR>
Display information about the search.<BR>
Display the matching string using the English variable names.
<BR>
Display the matching string using the standard Perl special variables.</I>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<B>Listing 15.8 15LST01.PL-Using the English Module
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
use English;
use strict;
my($searchSpace) = "TTTT BBBABBB DDDD";
my($pattern) = "B+AB+";
$searchSpace =~ m/$pattern/;
print("Search space: $searchSpace\n");
print("Pattern: /$pattern/\n");
print("Matched String: $English::MATCH\n"); # the English variable
print("Matched String: $&\n"); # the standard Perl variable
</PRE>
</BLOCKQUOTE>
<HR>
<P>
This program displays
<BLOCKQUOTE>
<PRE>
Search space: TTTT BBBABBB DDDD
Pattern: /B+AB+/
Matched String: BBBABBB
Matched String: BBBABBB
</PRE>
</BLOCKQUOTE>
<P>
You can see that the <TT>$&</TT>
and <TT>$MATCH</TT> variables are
equivalent. This means that you can use another programmer's fuNCtions
without renaming their variables and still use the English names
in your own fuNCtions.
<H3><A NAME="ExampleTheTTFONTSIZEFACECourierEnvFONTTTFONTSIZEModuleFONT">
Example: The <TT>Env </TT>Module</FONT>
</A></H3>
<P>
If you use environment variables a lot, then you need to look
at the <TT>Env</TT> module. It will
enable you to directly access the environment variables as Perl
scalar variables instead of through the <TT>%Env</TT>
hash. For example, <TT>$PATH</TT>
is equivalent to <TT>$ENV{'PATH'}</TT>.
<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>Load the </I><TT><I>Env</I></TT><I>
module.<BR>
Invoke the strict pragma.<BR>
Declare the </I><TT><I>@files</I></TT><I>
variable.<BR>
Open the temporary directory and read all of its files.<BR>
Display the name of the temporary directory.<BR>
Display the names of all files that end in tmp.</I>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<B>Listing 15.9 15LST09.PL-Displaying Temporary Files
Using the </B><TT><B><FONT FACE="Courier">Env</FONT></B></TT><B>
Module<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
use Env;
use strict;
my(@files);
opendir(DIR, $main::TEMP);
@files = readdir(DIR);
closedir(DIR);
print "$main::TEMP\n";
foreach (@files) {
print("\t$_\n") if m/\.tmp/i;
}
</PRE>
</BLOCKQUOTE>
<HR>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
C:\WINDOWS\TEMP
~Df182.TMP
~Df1B3.TMP
~Df8073.TMP
~Df8074.TMP
~WRS0003.tmp
~Df6116.TMP
~DFC2C2.TMP
~Df9145.TMP
</PRE>
</BLOCKQUOTE>
<P>
This program is pretty self-explanatory, except perhaps for the
manner in which the <TT>$main::TEMP</TT>
variable is specified. The <TT>strict</TT>
pragma requires all variables to be lexically declared or to be
fully qualified. The environment variables are declared in the
<TT>Env</TT> package, but exported
into the <TT>main</TT> namespace.
Therefore, they need to be qualified using the <TT>main::</TT>
notation.
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>
Summary</FONT></A></H2>
<P>
In this chapter, you learned about Perl modules. You read about
several guidelines that should be followed when creating modules.
For example, package name should have their first letter capitalized
and use file extensions of <TT>pm</TT>.
<P>
The <TT>require</TT> compiler directive
is used to load Perl libraries that were distributed with Perl
4. Modules, however, are loaded with the <TT>use</TT>
directive. In addition to loading the module, <TT>use</TT>
will move variable and fuNCtion names into the <TT>main</TT>
namespace where your script can easily access them. The name movement
is done by using the <TT>@EXPORT</TT>
and <TT>@EXPORT_OK</TT> arrays.
<P>
Next, you read about the <TT>BEGIN</TT>
and <TT>END</TT> blocks which are
like module constructors and destructors. The <TT>BEGIN</TT>
block is evaluated as soon as it is defined. <TT>END</TT>
blocks are evaluated just before your program ends-in reverse
order. The last <TT>END</TT> block
defined is the first to be evaluated.
<P>
Symbols tables are used to hold the fuNCtion and variable names
for each package. You learned that each symbol table is stored
in a hash named after the package name. For example, the symbol
table for the <TT>Room</TT> package
is stored in <TT>%Room::</TT>. Listing
15.3 contained a fuNCtion-<TT>dispSymbol</TT>-that
displays all of the names in a given symbol table.
<P>
Libraries are loaded using the <TT>require</TT>
compiler directive and modules are loaded with the <TT>use</TT>
directive. Unlike the <TT>require</TT>
directive, <TT>use</TT> will automatically
call a module's <TT>import()</TT>
fuNCtion to move fuNCtion and variable names from the module's
namespace into the <TT>main</TT> namespace.
The name movement is controlled using the <TT>@EXPORT</TT>
and <TT>@EXPORT_OK</TT> array. Names
in <TT>@EXPORT</TT> are always exported.
Those in <TT>@EXPORT_OK</TT> must
be explicitly mentioned in the <TT>use</TT>
statement.
<P>
The <TT>use</TT> directive also controls
other directives which are called pragmas. The most useful pragmas
are <TT>integer</TT> and <TT>strict</TT>.
Use the <TT>integer</TT> pragma when
you need fast integer math. And use <TT>strict</TT>
all of the time to enforce good programming habits-like using
local variables.
<P>
Table 15.2 shows the 25 modules that are distributed with Perl.
And then some more light was shed on how the <TT>my()</TT>
fuNCtion won't create variables that are local to a package. In
order to create variables in the packages' namespace, you need
to fully qualify them with the package name. For example, <TT>$Math::PI</TT>
or <TT>$Room::numChairs</TT>.
<P>
The last section of the chapter looked at specific examples of
how to use modules. The <TT>Carp</TT>,
<TT>English</TT>, and <TT>Env</TT>
modules were discussed. <TT>Carp</TT>
defines three fuNCtions: <TT>carp()</TT>,
<TT>croak()</TT>, and <TT>confess()</TT>
that aid in debugging and error handling. <TT>English</TT>
provides aliases for all of Perl's special variables so that Perl
code is easier to understand. <TT>Env</TT>
provides aliases for environmental variables so that you can access
them directly instead of through the <TT>%Env</TT>
hash variable.
<P>
In the next chapter, you learn about debugging Perl code. You
read about syntax or compile-time errors versus runtime errors.
The <TT>strict</TT> pragma will be
discussed in more detail.
<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>What is a module?
<LI>How is a module different from a library?
<LI>What is the correct file extension for a module?
<LI>What is a pragma?
<LI>What is the most important pragma and why?
<LI>What does the <TT>END</TT> block
do?
<LI>What is a symbol table?
<LI>How can you create a variable that is local to a package?
</OL>
<H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>
Review Exercises</FONT></A></H2>
<OL>
<LI>Write a program that uses <TT>BEGIN</TT>
and <TT>END</TT> blocks to write a
message to a log file about the start and end times for the program.
<LI>Use the <TT>English</TT> module
to display Perl's version number.
<LI>Modify the <TT>dispSymbols()</TT>
fuNCtion from Listing 15.3 to display only fuNCtion and variable
names passed as arguments.
<LI>Execute the program in Listing 15.5 with the -w command line
option. Describe the results.
<LI>Write a module to calculate the area of a rectangle. Use the
<TT>@EXPORT</TT> array to export the
name of your fuNCtion.
</OL>
<HR>
<CENTER><P><A HREF="ch14.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch14.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="ch16.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch16.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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -