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

📄 ch2.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$:</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$FORMAT_LINE_BREAK_chARACTERS</FONT></TT>

</TD><TD WIDTH=245>The set of characters after which a string can be broken up to fill with continuation characters

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^L</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$FORMAT_FORMFEED</FONT></TT>

</TD><TD WIDTH=245>The default form feed operator</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^A</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$AccUMULATOR</FONT></TT>

</TD><TD WIDTH=245>The current format line accumulator for <TT><FONT FACE="Courier">format()</FONT></TT> lines

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$?</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$chILD_ERROR</FONT></TT>

</TD><TD WIDTH=245>The status from the last tilde command</TD>

</TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$!</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$ERRNO</FONT></TT></TD>

<TD WIDTH=245>The last <TT><FONT FACE="Courier">errno</FONT></TT> value

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$@</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$EVAL_ERROR</FONT></TT>

</TD><TD WIDTH=245>The Perl error message from the last <TT><FONT FACE="Courier">eval</FONT></TT> statement

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$$</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$PROCESS_ID</FONT></TT>

</TD><TD WIDTH=245>The process number of this Perl script</TD>

</TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$&lt;</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$REAL_USER_ID</FONT></TT>

</TD><TD WIDTH=245>The real UID of this process</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$&gt;</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$EFFECTIVE_USER_ID</FONT></TT>

</TD><TD WIDTH=245>The effective UID of this process </TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$(</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$REAL_GROUP_ID</FONT></TT>

</TD><TD WIDTH=245>The real group GID of this process</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$)</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$EFFECTIVE_GROUP_ID</FONT></TT>

</TD><TD WIDTH=245>The effective GID of this process</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$0</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$PROGRAM_NAME</FONT></TT>

</TD><TD WIDTH=245>The name of the program in <TT><FONT FACE="Courier">$ARGV[0]</FONT></TT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$[</FONT></TT></TD>

<TD WIDTH=245>n/a</TD><TD WIDTH=245>Index of the first element in the array

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$]</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$PERL_VERSION</FONT></TT>

</TD><TD WIDTH=245>The Perl version string</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^D</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$DEBUGGING</FONT></TT>

</TD><TD WIDTH=245>The current value of the debugging flag</TD>

</TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^F</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$SYSTEM_FD_MAX</FONT></TT>

</TD><TD WIDTH=245>The maximum file descriptors in the system (RO)

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^I</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$INPLACE_EDIT</FONT></TT>

</TD><TD WIDTH=245>The in-place edit extension</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^P</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$PERLDB</FONT></TT></TD>

<TD WIDTH=245>The value of the internal debugger flag</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^T</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$BASETIME</FONT></TT></TD>

<TD WIDTH=245>The time at which the debugged script started running

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^W</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$WARNING</FONT></TT></TD>

<TD WIDTH=245>The value of the <TT><FONT FACE="Courier">-w</FONT></TT> switch

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^X</FONT></TT></TD>

<TD WIDTH=245><TT><FONT FACE="Courier">$EXECUTABLE_NAME</FONT></TT>

</TD><TD WIDTH=245>The name of the program in <TT><FONT FACE="Courier">$ARGV[0]</FONT></TT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$ARGV</FONT></TT></TD>

<TD WIDTH=245>n/a</TD><TD WIDTH=245>The name of the current file while reading from the <TT><FONT FACE="Courier">&lt;&gt;</FONT></TT> in a <TT><FONT FACE="Courier">while</FONT></TT> loop

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$VERSION</FONT></TT>

</TD><TD WIDTH=245>n/a</TD><TD WIDTH=245>The version number of the Perl interpreter

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">%ENV</FONT></TT></TD>

<TD WIDTH=245>n/a</TD><TD WIDTH=245>The hash of the environment variables for the process 

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">%Inc</FONT></TT></TD>

<TD WIDTH=245>n/a</TD><TD WIDTH=245>The hash of filenames that have been included in the current file

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">%SIG</FONT></TT></TD>

<TD WIDTH=245>n/a</TD><TD WIDTH=245>The hash of all signal handlers for the current process

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">@ARGV</FONT></TT></TD>

<TD WIDTH=245>n/a</TD><TD WIDTH=245>The command-line arguments for the script

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">@EXPORT</FONT></TT>

</TD><TD WIDTH=245>n/a</TD><TD WIDTH=245>The names of all exported functions in a module

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">@F</FONT></TT></TD>

<TD WIDTH=245>n/a</TD><TD WIDTH=245>The command-line options used for the current program

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">@Inc</FONT></TT></TD>

<TD WIDTH=245>n/a</TD><TD WIDTH=245>The pathnames of places to look in for all included files

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">@ISA</FONT></TT></TD>

<TD WIDTH=245>n/a</TD><TD WIDTH=245>The names of all modules to search for when looking for a module

</TD></TR>

</TABLE></CENTER>

<P>

<P>

Don't worry if you do not recognize some of these strange characters.

I will be covering them all in the course of this book. 

<P>

Now let's see how you can use these built-in variables as well

as your own variables in code.

<H3><A NAME="CodeBlocks">Code Blocks</A></H3>

<P>

Variables and assignment statements exist in code blocks. Each

code block is a section of code between two curly braces. Recognizing

code blocks matters when you are concerned about the scope of

influence of code on the value of a variable. (More on scope in

a moment.) Code blocks are simply assignment statements enclosed

between curly braces. Normally, you see code blocks in loop constructs

and conditionals. It's syntactically correct to use statements

like this in Perl programs:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">{<BR>

<I>print something;<BR>

print more of something; <BR>

more statements;<BR>

</I>}</FONT></TT>

</BLOCKQUOTE>

<P>

This coding style is rare and is usually done only if the programmer

explicitly wants to keep some special variables within the curly

braces. Usually, most of the application's code will be in one

type of block, either a subroutine, loop, or conditional, with

only the lines not in such blocks being those that are global

to the rest of the components of the program. 

<P>

Here are some examples of code blocks available in Perl:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">{<BR>

# a simple code block with statements in here. <BR>

}<BR>

<BR>

while(<I>condition</I>) {<BR>

&nbsp;&nbsp;&nbsp;&nbsp;... <I>execute code here while condition

is true</I>;<BR>

}<BR>

<BR>

until(condition) {&nbsp;&nbsp;# opposite of while statement.<BR>

&nbsp;&nbsp;&nbsp;&nbsp;... <I>execute code here while condition

is false</I>;<BR>

}<BR>

<BR>

do {<BR>

&nbsp;&nbsp;&nbsp;&nbsp;... <I>do this at least once</I> ...<BR>

&nbsp;&nbsp;&nbsp;&nbsp;... <I>stop if condition is false</I>

...<BR>

} while(condition);<BR>

<BR>

do {<BR>

&nbsp;&nbsp;&nbsp;&nbsp;... <I>do this at least once</I> ...<BR>

&nbsp;&nbsp;&nbsp;&nbsp;... <I>stop if condition is true</I> ...

<BR>

} until(condition);<BR>

<BR>

if (condition1) {<BR>

&nbsp;&nbsp;&nbsp;&nbsp;<I>condition1_code true</I>;<BR>

} else {<BR>

...&nbsp;&nbsp;&nbsp;&nbsp;<I>no condition1 up to conditionN&nbsp;&nbsp;is

true</I>;<BR>

}<BR>

<BR>

if (condition1) {<BR>

...&nbsp;&nbsp;&nbsp;&nbsp;<I>condition1_code true</I>;<BR>

} elsif (condition2) {<BR>

&nbsp;&nbsp;&nbsp;&nbsp;<I>condition1_code true</I>;<BR>

....<BR>

} elsif (conditionN) {<BR>

&nbsp;&nbsp;&nbsp;&nbsp;<I>conditionN_code true</I>;<BR>

} else {<BR>

...&nbsp;&nbsp;&nbsp;&nbsp;<I>no condition from 1 up to N&nbsp;&nbsp;is

true</I>;<BR>

}<BR>

<BR>

unless (condition1) { # opposite of &quot;if&quot; statement.

<BR>

...&nbsp;&nbsp;&nbsp;&nbsp;<I>do this if condition is false</I>;

<BR>

}</FONT></TT>

</BLOCKQUOTE>

<P>

The condition in these blocks of code is anything from a Perl

variable to an expression that returns either a <TT><FONT FACE="Courier">true</FONT></TT>

or <TT><FONT FACE="Courier">false</FONT></TT> value. A <TT><FONT FACE="Courier">true</FONT></TT>

value is a non-zero value or a non-empty string.

<P>

Code blocks can be declared within code blocks to create levels

of code blocks. Variables declared in one code block are usually

global to the rest of the program. To keep the scope of the variable

limited to the code block in which it is declared, use the <TT><FONT FACE="Courier">my

$variableName</FONT></TT> syntax. If you declare with <TT><FONT FACE="Courier">local

$variableName</FONT></TT> syntax, the <TT><FONT FACE="Courier">$variableName</FONT></TT>

will be available to all lower levels but not outside the code

block.

<P>

Figure 2.1 illustrates how the scoping rules work in Perl. The

main block declares two variables, <TT><FONT FACE="Courier">$x</FONT></TT>

and <TT><FONT FACE="Courier">$y</FONT></TT>. There are two blocks

of code between curly braces, block A and block B. The variable

<TT><FONT FACE="Courier">$x</FONT></TT> is not available to either

of these blocks, but <TT><FONT FACE="Courier">$y</FONT></TT> will

be available. 

<P>

<A HREF="f2-1.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f2-1.gif" ><B>Figure 2.1 : </B><I>Scoping rules in Perl </I></A>

<P>

Because block A is declared in the main block, the code in it

will be able to access <TT><FONT FACE="Courier">$y</FONT></TT>

but not <TT><FONT FACE="Courier">$x</FONT></TT> because <TT><FONT FACE="Courier">$x</FONT></TT>

is declared as <TT><FONT FACE="Courier">&quot;my&quot;</FONT></TT>.

The variable <TT><FONT FACE="Courier">$f</FONT></TT> will not

be available to other blocks of code even if they are declared

within block A. The variable <TT><FONT FACE="Courier">$g</FONT></TT>

is not declared as <TT><FONT FACE="Courier">&quot;local&quot;</FONT></TT>

or <TT><FONT FACE="Courier">&quot;my&quot;</FONT></TT>, so it's

not visible to the main module nor to block B. 

<P>

The code in block B declares two variables, <TT><FONT FACE="Courier">$k</FONT></TT>

and <TT><FONT FACE="Courier">$m</FONT></TT>. The variable <TT><FONT FACE="Courier">$k</FONT></TT>

can be assigned the value of <TT><FONT FACE="Courier">$g</FONT></TT>,

provided that the code in block A is called before the code in

block B. If the code in block B is called before the code in block

A, the variable <TT><FONT FACE="Courier">$g</FONT></TT> will not

be declared, and a value of <TT><FONT FACE="Courier">'undef'</FONT></TT>

will be assigned to <TT><FONT FACE="Courier">$k</FONT></TT>. Also,

<TT><FONT FACE="Courier">$m</FONT></TT> cannot use the value of

<TT><FONT FACE="Courier">$f</FONT></TT> because <TT><FONT FACE="Courier">$f</FONT></TT>

is declared in block A as a <TT><FONT FACE="Courier">&quot;my&quot;</FONT></TT>

variable. The values of <TT><FONT FACE="Courier">$y</FONT></TT>

and <TT><FONT FACE="Courier">$g</FONT></TT> are available to code

in block B. 

<P>

Finally, another code block (call it C) could be assigned in block

B. Block C is not shown in the figure. All variables in this new

block C that are declared as neither <TT><FONT FACE="Courier">&quot;my&quot;</FONT></TT>

nor <TT><FONT FACE="Courier">&quot;local&quot;</FONT></TT> would

be available to blocks A and B and the main program. Code in block

C would not be able to access variables <TT><FONT FACE="Courier">$f</FONT></TT>,

<TT><FONT FACE="Courier">$k</FONT></TT>, and <TT><FONT FACE="Courier">$m</FONT></TT>

because they are declared as <TT><FONT FACE="Courier">&quot;my&quot;</FONT></TT>.

The variable <TT><FONT FACE="Courier">$g</FONT></TT> would not

be available to code in block B or C because it is local to block

A.

<P>

Keep in mind that variables in code blocks are also declared at

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -