⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch15.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 4 页
字号:
</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>$&lt;</TT></CENTER></TD><TD WIDTH=451><TT>$REAL_USER_ID</TT> or <TT>$UID</TT></TD></TR><TR><TD WIDTH=139><CENTER><TT>$&gt;</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 variablesto access information about a matched string.<P><IMG SRC="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&nbsp;&nbsp;15LST01.PL-Using the English Module<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>use English;use strict;my($searchSpace) = &quot;TTTT BBBABBB DDDD&quot;;my($pattern)     = &quot;B+AB+&quot;;$searchSpace =~ m/$pattern/;print(&quot;Search space:   $searchSpace\n&quot;);print(&quot;Pattern:        /$pattern/\n&quot;);print(&quot;Matched String: $English::MATCH\n&quot;);  # the English variableprint(&quot;Matched String: $&amp;\n&quot;);               # the standard Perl variable</PRE></BLOCKQUOTE><HR><P>This program displays<BLOCKQUOTE><PRE>Search space:   TTTT BBBABBB DDDDPattern:        /B+AB+/Matched String: BBBABBBMatched String: BBBABBB</PRE></BLOCKQUOTE><P>You can see that the <TT>$&amp;</TT>and <TT>$MATCH</TT> variables areequivalent. This means that you can use another programmer's fuNCtionswithout renaming their variables and still use the English namesin 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 lookat the <TT>Env</TT> module. It willenable you to directly access the environment variables as Perlscalar 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" 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&nbsp;&nbsp;15LST09.PL-Displaying Temporary FilesUsing 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 &quot;$main::TEMP\n&quot;;foreach (@files) {    print(&quot;\t$_\n&quot;) 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 themanner in which the <TT>$main::TEMP</TT>variable is specified. The <TT>strict</TT>pragma requires all variables to be lexically declared or to befully qualified. The environment variables are declared in the<TT>Env</TT> package, but exportedinto 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 aboutseveral guidelines that should be followed when creating modules.For example, package name should have their first letter capitalizedand use file extensions of <TT>pm</TT>.<P>The <TT>require</TT> compiler directiveis used to load Perl libraries that were distributed with Perl4. 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 movementis 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 arelike 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 reverseorder. The last <TT>END</TT> blockdefined is the first to be evaluated.<P>Symbols tables are used to hold the fuNCtion and variable namesfor each package. You learned that each symbol table is storedin a hash named after the package name. For example, the symboltable for the <TT>Room</TT> packageis stored in <TT>%Room::</TT>. Listing15.3 contained a fuNCtion-<TT>dispSymbol</TT>-thatdisplays 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 automaticallycall a module's <TT>import()</TT>fuNCtion to move fuNCtion and variable names from the module'snamespace into the <TT>main</TT> namespace.The name movement is controlled using the <TT>@EXPORT</TT>and <TT>@EXPORT_OK</TT> array. Namesin <TT>@EXPORT</TT> are always exported.Those in <TT>@EXPORT_OK</TT> mustbe explicitly mentioned in the <TT>use</TT>statement.<P>The <TT>use</TT> directive also controlsother directives which are called pragmas. The most useful pragmasare <TT>integer</TT> and <TT>strict</TT>.Use the <TT>integer</TT> pragma whenyou need fast integer math. And use <TT>strict</TT>all of the time to enforce good programming habits-like usinglocal 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. Inorder to create variables in the packages' namespace, you needto 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 ofhow 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 Perlcode is easier to understand. <TT>Env</TT>provides aliases for environmental variables so that you can accessthem directly instead of through the <TT>%Env</TT>hash variable.<P>In the next chapter, you learn about debugging Perl code. Youread about syntax or compile-time errors versus runtime errors.The <TT>strict</TT> pragma will bediscussed 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> blockdo?<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 amessage to a log file about the start and end times for the program.<LI>Use the <TT>English</TT> moduleto display Perl's version number.<LI>Modify the <TT>dispSymbols()</TT>fuNCtion from Listing 15.3 to display only fuNCtion and variablenames passed as arguments.<LI>Execute the program in Listing 15.5 with the -w command lineoption. Describe the results.<LI>Write a module to calculate the area of a rectangle. Use the<TT>@EXPORT</TT> array to export thename of your fuNCtion.</OL><HR><CENTER><P><A HREF="ch14.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch16.htm"><IMG SRC="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 + -