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

📄 ch12.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 3 页
字号:
As you no doubt realize by now, Perl has some really odd features,and  the <TT>DATA</TT> file handleis one of them. This file handle lets you store read-only datain the same file as your Perl script, which might come in handyif you need to send both code and data to someone via e-mail.<P>When using the <TT>DATA</TT> filehandle, you don't need to open or close the file handle-just startreading from the file handle using the diamond operator. The followingsimple example shows you how to use the <TT>DATA</TT>file handle.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Read all the lines that follow the line containing </I><TT><I>__END__</I></TT><I>.<BR>Loop through the </I><TT><I>@lines</I></TT><I>array, printing each element.<BR>Everything above the </I><TT><I>__END__</I></TT><I>line is code; everything below is data.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>@lines = &lt;DATA&gt;;foreach (@lines) {    print(&quot;$_&quot;);}__END__Line oneLine twoLine three</PRE></BLOCKQUOTE><P>This program displays the following:<BLOCKQUOTE><PRE>Line oneLine twoLine three</PRE></BLOCKQUOTE><H4>Example: Using the <TT><I>%ENV</I></TT>Variable</H4><P><I>Environment variables</I> are used by the operating systemto store bits of information that are needed to run the computer.They are called environment variables because you rarely needto use them and because they simply remain in the background-justanother part of the overall computing environment of your system.When your Perl process is started, it is given a copy of the environmentvariables to use as needed.<P>You can change the environment variables, but the changes willnot persist after the process running Perl is ended. The changeswill, however, affect the current process and any child processesthat are started.<P>You can print out the environment variables by using these linesof code:<BLOCKQUOTE><PRE>foreach $key (keys(%ENV)) {    printf(&quot;%-10.10s: $ENV{$key}\n&quot;, $key);}</PRE></BLOCKQUOTE><P>On my Windows 95 machine, this program displays the following:<BLOCKQUOTE><PRE>WINBOOTDIR: C:\WINDOWSTMP       : C:\WINDOWS\TEMPPROMPT    : $p$gCLASSPATH : .\;e:\jdk\classes;TEMP      : C:\WINDOWS\TEMPCOMSPEC   : C:\WINDOWS\COMMAND.COMCMDLINE   : perl -w 12lst01.plBLASTER   : A220 I10 D3 H7 P330 T6WINDIR    : C:\WINDOWSPATH      : C:\WINDOWS;C:\WINDOWS\COMMAND;C:\PERL5\BIN;TZ        : GMT-05:00</PRE></BLOCKQUOTE><P>Only a few of these variables are interesting. The <TT>TMP</TT>and <TT>TEMP</TT> variables let youknow where temporary files should be placed. The <TT>PATH</TT>variable lets the system know where to look for executable programs.It will search each directory in the list until the needed fileis found. The <TT>TZ</TT> variablelets you know which time zone the computer is running in.<P>The most useful variable is probably the <TT>PATH</TT>statement. By changing it, you can force the system to searchthe directories you specify. This might be useful if you suspectthat another program of the same name resides in another directory.By placing the current directory at the beginning of the <TT>PATH</TT>variable, it will be searched first and you'll always get theexecutable you want. For example:<BLOCKQUOTE><PRE>$ENV{&quot;PATH&quot;} = &quot;.;&quot; . $ENV{&quot;PATH&quot;};</PRE></BLOCKQUOTE><P>A single period is used to refer to the current directory, anda semicolon is used to delimit the directories in the <TT>PATH</TT>variable. So this statement forces the operating system to lookin the current directory before searching the rest of the directoriesin <TT>PATH</TT>.<P>Environment variables can be useful if you want a quick way topass information between a parent and a child process. The parentcan set the variables, and the child can read it.<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A></H2><P>This chapter gathered into one location all the special variablesused by Perl. Most of the variables have already been discussedin previous chapters, and a few will be discussed in later chapters.<P>Table 12.1 was organized to follow the <TT>PERLVAR.htm</TT>document that comes in the Perl distribution, so if you aren'tfamiliar with a variable used in someone else's code, that's theplace to look. The variables are basically ordered alphabetically.<P>Table 12.2 was organized according to fuNCtionality. Some variablesare used with files, some with arrays, and so forth.<P>You saw an example of how to use the <TT>DATA</TT>file handle to read information from the same file that holdsthe Perl script.<P>The <TT>%ENV</TT> variable was alsodiscussed. This hash is used to hold the environmental variablesused mostly by the operating system.<P>In the next chapter, &quot;Handling Errors and Signals,&quot;you learn about how to handle error conditions, use the <TT>eval()</TT>fuNCtion, and other things dealing with exceptions that can happenwhile your program runs.<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 the <TT>$/</TT> variableused for?<LI>What file handle is used to avoid a second system call whendoing two or more file tests?<LI>What will the following program display?<BR><BR><TT>$_ = &quot;The big red shoe&quot;;<BR>m/[rs].*\b/;<BR>print(&quot;$`\n&quot;);</TT><LI>What variable holds the value of the last match string?<LI>What will the following program display?<BR><BR><TT>@array = (1..5);<BR>$&quot; = &quot;+&quot;;<BR>print(&quot;@array\n&quot;);</TT><LI>What does the following program display?<BR><TT>@array = ('A'..'E');<BR><BR>foreach (@array) {<BR>    print();<BR>}<BR><BR>$\ = &quot;\n&quot;;<BR>foreach (@array) {<BR>    print();<BR>}</TT></OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>Review Exercises</FONT></A></H2><OL><LI>Write a program that changes the array element separator usedin interpolation of arrays inside double-quoted strings to bea comma instead of a space.<LI>Write a program that displays which version of the Perl interpreteryou are running.<LI>Create a file in your temporary directory. (Hint: use the<TT>%ENV</TT> special variable.)<LI>Write a program that uses the <TT>$\</TT>to end each printed element with an <TT>&quot;:END&quot;</TT>string.<LI>Write a program that prints the last record in a file. Therecords should be variable-length, but each record starts withthe string <TT>&quot;START:&quot;</TT>.(Hint: look at the <TT>$/</TT> variable.)</OL><HR><CENTER><P><A HREF="ch11.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="ch13.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 + -