📄 ch15.htm
字号:
</TD></TR></TABLE></CENTER><P><H2><A NAME="TheStandardModules"><FONT SIZE=5 COLOR=#FF0000>The Standard Modules</FONT></A></H2><P>Table 15.2 lists the modules that should come with all distributionsof Perl. Some of these modules are not portable across all operatingsystems, however. The descriptions for the modules mention theiNCompatibility if I know about it.<BR><P><CENTER><B>Table 15.2 Perl's Standard Modules</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=153><I>Module</I></TD><TD WIDTH=437><I>Description</I></TD></TR><TR><TD WIDTH=153>Text::Abbrev</TD><TD WIDTH=437>Creates an abbreviation table from a list. The abbreviation table consists of the shortest sequeNCe of characters that can uniquely identify each element of the list.</TD></TR><TR><TD WIDTH=153>AnyDBM_File</TD><TD WIDTH=437>Provides a framework for accessing multiple DBMs. This is a UNIX-based module.</TD></TR><TR><TD WIDTH=153>AutoLoader</TD><TD WIDTH=437>Loads fuNCtions on demand. This enables your scripts to use less memory.</TD></TR><TR><TD WIDTH=153>AutoSplit</TD><TD WIDTH=437>Splits a package or module into its component parts for autoloading.</TD></TR><TR><TD WIDTH=153>BeNChmark</TD><TD WIDTH=437>Tracks the running time of code. This module can be modified to run under Windows but some of its fuNCtionality will be lost.</TD></TR><TR><TD WIDTH=153>Carp</TD><TD WIDTH=437>Provides an alternative to the <TT>warn()</TT> and <TT>die()</TT> fuNCtions that report the line number of the calling routine. See "Example: The <TT>Carp</TT> Module" later in the chapter for more information.</TD></TR><TR><TD WIDTH=153>I18N::Collate</TD><TD WIDTH=437>Compares 8-bit scalar data according to the current locale. This helps to give an international viewpoint to your script.</TD></TR><TR><TD WIDTH=153>Config</TD><TD WIDTH=437>Accesses the Perl configuration options.</TD></TR><TR><TD WIDTH=153>Cwd</TD><TD WIDTH=437>Gets the pathname of the current working directory. This module will generate a warning message when used with the -w command line option under the Windows and VAX VMS operating systems. You can safely ignore the warning.</TD></TR><TR><TD WIDTH=153>Dynaloader</TD><TD WIDTH=437>Lets you dynamically load C libraries into Perl code.</TD></TR><TR><TD WIDTH=153>English</TD><TD WIDTH=437>Lets you use English terms instead of the normal special variable names.</TD></TR><TR><TD WIDTH=153>Env</TD><TD WIDTH=437>Lets you access the system environment variables using scalars instead of a hash. If you make heavy use of the environment variables, this module might improve the speed of your script.</TD></TR><TR><TD WIDTH=153>Exporter</TD><TD WIDTH=437>Controls namespace manipulations.</TD></TR><TR><TD WIDTH=153>Fcntl</TD><TD WIDTH=437>Loads file control definition used by the <TT>fcntl()</TT> fuNCtion.</TD></TR><TR><TD WIDTH=153>FileHandle</TD><TD WIDTH=437>Provides an object-oriented interface to filehandles.</TD></TR><TR><TD WIDTH=153>File::Basename</TD><TD WIDTH=437>Separates a file name and path from a specification.</TD></TR><TR><TD WIDTH=153>File::CheckTree</TD><TD WIDTH=437>Runs filetest checks on a directory tree.</TD></TR><TR><TD WIDTH=153>File::Find</TD><TD WIDTH=437>Traverse a file tree. This module will not work under the Windows operating systems without modification.</TD></TR><TR><TD WIDTH=153>Getopt</TD><TD WIDTH=437>Provides basic and extended options processing.</TD></TR><TR><TD WIDTH=153>ExtUtils::MakeMaker</TD><TD WIDTH=437>Creates a Makefile for a Perl extension.</TD></TR><TR><TD WIDTH=153>Ipc::Open2</TD><TD WIDTH=437>Opens a process for both reading and writing.</TD></TR><TR><TD WIDTH=153>Ipc::Open3</TD><TD WIDTH=437>Opens a process for reading, writing, and error handling.</TD></TR><TR><TD WIDTH=153>POSIX</TD><TD WIDTH=437>Provides an interface to IEEE 1003.1 namespace.</TD></TR><TR><TD WIDTH=153>Net::Ping</TD><TD WIDTH=437>Checks to see if a host is available.</TD></TR><TR><TD WIDTH=153>Socket</TD><TD WIDTH=437>Loads socket definitions used by the socket fuNCtions.</TD></TR></TABLE></CENTER><P><H2><A NAME="TTFONTSIZEFACECourierstrictmyFONTTTFONTSIZEandModulesFONT"><FONT SIZE=5 COLOR=#FF0000><TT>strict, my() </TT>andModules</FONT></FONT></A></H2><P>In order to use the <TT>strict</TT>pragma with modules, you need to know a bit more about the <TT>my()</TT>fuNCtion about how it creates lexical variables instead of localvariables. You may be tempted to think that variables declaredwith <TT>my()</TT> are local to apackage, especially siNCe you can have more than one package statementper file. However, <TT>my()</TT> doesthe exact opposite; in fact, variables that are declared with<TT>my()</TT> are never stored insidethe symbol table.<P>If you need to declare variables that are local to a package,fully qualify your variable name in the declaration or initializationstatement, like this:<BLOCKQUOTE><PRE>use strict;$main::foo = '';package Math; $Math::PI = 3.1415 && $Math::PI;</PRE></BLOCKQUOTE><P>This code snippet declares two variables: <TT>$foo</TT>in the <TT>main</TT> namespace and<TT>$PI</TT> in the <TT>Math</TT>namespace. The <TT>&& $Math::PI</TT>part of the second declaration is used to avoid getting errormessages from the -w command line option. SiNCe the variable isinside a package, there is no guarantee that it will be used bythe calling script and the -w command line option generates awarning about any variable that is only used oNCe. By adding theharmless logical and to the declaration, the warning messagesare avoided.<H2><A NAME="ModuleExamples"><FONT SIZE=5 COLOR=#FF0000>Module Examples</FONT></A></H2><P>This section shows you how to use the <TT>Carp</TT>,<TT>English</TT>, and <TT>Env</TT>modules. After looking at these examples, you should feel comfortableabout trying the rest.<H3><A NAME="ExampleTheTTFONTSIZEFACECourierCarpFONTTTFONTSIZEModuleFONT">Example: The <TT>Carp</TT>Module</FONT></A></H3><P>This useful little module lets you do a better job of analyzingruntime errors-like when your script can't open a file or whenan unexpected input value is found. It defines the <TT>carp()</TT>,<TT>croak()</TT>, and <TT>confess()</TT>fuNCtions. These are similar to <TT>warn()</TT>and <TT>die()</TT>. However, insteadof reported in the exact script line where the error occurred,the fuNCtions in this module will display the line number thatcalled the fuNCtion that generated the error. Confused? So wasI, until I did some experimenting. The results of that experimentingcan be found in Listing 15.6.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Load the Carp module.<BR>Invoke the strict pragma.<BR>Start the Foo namespace.<BR>Define the </I><TT><I>foo()</I></TT><I>fuNCtion.<BR>Call the </I><TT><I>carp()</I></TT><I>fuNCtion.<BR>Call the </I><TT><I>croak()</I></TT><I>fuNCtion.<BR>Switch to the main namespace.<BR>Call the </I><TT><I>foo()</I></TT><I>fuNCtion.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 15.6 15LST06.PL-Using the </B><TT><B><FONT FACE="Courier">carp()</FONT></B></TT><B>and </B><TT><B><FONT FACE="Courier">croak()</FONT></B></TT><B>from the </B><TT><B><FONT FACE="Courier">Carp Module<BR></FONT></B></TT></BLOCKQUOTE><BLOCKQUOTE><PRE>use Carp;use strict;package Foo; sub foo { main::carp("carp called at line " . __LINE__ . ",\n but foo() was called"); main::croak("croak called at line " . __LINE__ . ",\n but foo() was called");}package main; foo::foo();</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>carp called at line 9, but foo() was called at e.pl line 18croak called at line 10, but foo() was called at e.pl line 18</PRE></BLOCKQUOTE><P>This example uses a compiler symbol, __LINE__, to iNCorporatethe current line number in the string passed to both <TT>carp()</TT>and <TT>croak()</TT>. This techniqueenables you to see both the line number where <TT>carp()</TT>and <TT>croak()</TT> were called <I>and</I>the line number where <TT>foo()</TT>was called.<P>The <TT>Carp</TT> module also definesa <TT>confess()</TT> fuNCtion whichis similar to <TT>croak()</TT> exceptthat a fuNCtion call history will also be displayed. Listing 15.7shows how this fuNCtion can be used. The fuNCtion declarationswere placed after the <TT>foo()</TT>fuNCtion call so that the program flow reads from top to bottomwith no jumping around.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Load the Carp module.<BR>Invoke the strict pragma.<BR>Call </I><TT><I>foo()</I></TT><I>.<BR>Define </I><TT><I>foo()</I></TT><I>.<BR>Call </I><TT><I>bar()</I></TT><I>.<BR>Define </I><TT><I>bar()</I></TT><I>.<BR>Call </I><TT><I>baz()</I></TT><I>.<BR>Define </I><TT><I>baz()</I></TT><I>.<BR>Call </I><TT><I>Confess()</I></TT><I>.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 15.7 15LST07.PL-Using </B><TT><B><FONT FACE="Courier">confess()</FONT></B></TT><B>from the </B><TT><B><FONT FACE="Courier">Carp</FONT></B></TT><B>Module<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>use Carp;use strict;foo();sub foo { bar();}sub bar { baz();}sub baz { confess("I give up!");}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>I give up! at e.pl line 16 main::baz called at e.pl line 12 main::bar called at e.pl line 8 main::foo called at e.pl line 5</PRE></BLOCKQUOTE><P>This daisy-chain of fuNCtion calls was done to show you how thefuNCtion call history looks when displayed. The fuNCtion callhistory is also called a <I>stack trace</I>. As each fuNCtionis called, the address from which it is called gets placed ona stack. When the <TT>confess()</TT>fuNCtion is called, the stack is unwound or read. This lets Perlprint the fuNCtion call history.<H3><A NAME="ExampleTheTTFONTSIZEFACECourierEnglishFONTTTFONTSIZEModuleFONT">Example: The <TT>English</TT>Module</FONT></A></H3><P>The <TT>English</TT> module is designedto make your scripts more readable. It creates aliases for allof the special variables that were discussed in <A HREF="ch12.htm" >Chapter 12</A>, "UsingSpecial Variables." Table 15.3 lists all of the aliases thatare defined. After the table, some examples show you how the aliasesare used.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>Some of the same coNCepts embodied by the special variables are used by the UNIX-based awk program. The <TT>English</TT> module also provides aliases that match what the special variables are called in awk.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>I think that this module is especially useful because it provides aliases for the regular expression matching special variables and the formatting special variables. You'll use the other special variables often enough so that their use becomes second nature. Or else you won't need to use them at all.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P><CENTER><B>Table 15.3 Aliases Provided by the EnglishModule</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=139><CENTER><I>Special Variable</I></CENTER></TD><TD WIDTH=451><I>Alias</I></TD></TR><TR><TD COLSPAN=2 WIDTH=590><B>Miscellaneous</B></TD></TR><TR><TD WIDTH=139><CENTER><TT>$_</TT></CENTER></TD><TD WIDTH=451><TT>$ARG</TT></TD></TR><TR><TD WIDTH=139><CENTER><TT>@_</TT></CENTER></TD><TD WIDTH=451><TT>@ARG</TT></TD></TR><TR><TD WIDTH=139><CENTER><TT>$"</TT></CENTER></TD><TD WIDTH=451><TT>$LIST_SEPARATOR</TT></TD></TR><TR><TD WIDTH=139><CENTER><TT>$;</TT></CENTER></TD><TD WIDTH=451><TT>$SUBSCRIPT_SEPARATOR</TT> or <TT>$SUBSEP</TT></TD></TR><TR><TD COLSPAN=2 WIDTH=590><B>Regular Expression or Matching</B></TD></TR><TR><TD WIDTH=139><CENTER><TT>$&</TT></CENTER></TD><TD WIDTH=451><TT>$MATCH</TT></TD></TR><TR><TD WIDTH=139><CENTER><TT>$`</TT></CENTER></TD><TD WIDTH=451><TT>$PREMATCH</TT></TD></TR><TR><TD WIDTH=139><CENTER><TT>$´</TT></CENTER></TD><TD WIDTH=451><TT>$POSTMATCH</TT></TD></TR><TR><TD WIDTH=139><CENTER><TT>$+</TT></CENTER></TD><TD WIDTH=451><TT>$LAST_PAREN_MATCH</TT></TD></TR><TR><TD COLSPAN=2 WIDTH=590><B>Input</B></TD></TR><TR><TD WIDTH=139><CENTER><TT>$.</TT></CENTER></TD><TD WIDTH=451><TT>$INPUT_LINE_NUMBER</TT> or <TT>$NR</TT></TD></TR><TR><TD WIDTH=139><CENTER><TT>$/</TT></CENTER>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -