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

📄 ch12.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 3 页
字号:

As you no doubt realize by now, Perl has some really odd features,

and  the <TT>DATA</TT> file handle

is one of them. This file handle lets you store read-only data

in the same file as your Perl script, which might come in handy

if you need to send both code and data to someone via e-mail.

<P>

When using the <TT>DATA</TT> file

handle, you don't need to open or close the file handle-just start

reading from the file handle using the diamond operator. The following

simple example shows you how to use the <TT>DATA</TT>

file handle.

<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>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 one

Line two

Line three

</PRE>

</BLOCKQUOTE>

<P>

This program displays the following:

<BLOCKQUOTE>

<PRE>

Line one

Line two

Line three

</PRE>

</BLOCKQUOTE>

<H4>Example: Using the <TT><I>%ENV</I></TT>

Variable</H4>

<P>

<I>Environment variables</I> are used by the operating system

to store bits of information that are needed to run the computer.

They are called environment variables because you rarely need

to use them and because they simply remain in the background-just

another part of the overall computing environment of your system.

When your Perl process is started, it is given a copy of the environment

variables to use as needed.

<P>

You can change the environment variables, but the changes will

not persist after the process running Perl is ended. The changes

will, however, affect the current process and any child processes

that are started.

<P>

You can print out the environment variables by using these lines

of 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:\WINDOWS

TMP       : C:\WINDOWS\TEMP

PROMPT    : $p$g

CLASSPATH : .\;e:\jdk\classes;

TEMP      : C:\WINDOWS\TEMP

COMSPEC   : C:\WINDOWS\COMMAND.COM

CMDLINE   : perl -w 12lst01.pl

BLASTER   : A220 I10 D3 H7 P330 T6

WINDIR    : C:\WINDOWS

PATH      : 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 you

know 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 file

is found. The <TT>TZ</TT> variable

lets 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 search

the directories you specify. This might be useful if you suspect

that 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 the

executable 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, and

a semicolon is used to delimit the directories in the <TT>PATH</TT>

variable. So this statement forces the operating system to look

in the current directory before searching the rest of the directories

in <TT>PATH</TT>.

<P>

Environment variables can be useful if you want a quick way to

pass information between a parent and a child process. The parent

can 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 variables

used by Perl. Most of the variables have already been discussed

in 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't

familiar with a variable used in someone else's code, that's the

place to look. The variables are basically ordered alphabetically.

<P>

Table 12.2 was organized according to fuNCtionality. Some variables

are 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 holds

the Perl script.

<P>

The <TT>%ENV</TT> variable was also

discussed. This hash is used to hold the environmental variables

used 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 happen

while 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> variable

used for?

<LI>What file handle is used to avoid a second system call when

doing 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 used

in interpolation of arrays inside double-quoted strings to be

a comma instead of a space.

<LI>Write a program that displays which version of the Perl interpreter

you 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. The

records should be variable-length, but each record starts with

the string <TT>&quot;START:&quot;</TT>.

(Hint: look at the <TT>$/</TT> variable.)

</OL>

<HR>



<CENTER><P><A HREF="ch11.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch11.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="ch13.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch13.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 + -