perlvar.html
来自「perl教程」· HTML 代码 · 共 1,313 行 · 第 1/5 页
HTML
1,313 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlvar - Perl predefined variables</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perlvar - Perl predefined variables</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#predefined_names">Predefined Names</a></li>
<li><a href="#error_indicators">Error Indicators</a></li>
<li><a href="#technical_note_on_the_syntax_of_variable_names">Technical Note on the Syntax of Variable Names</a></li>
</ul>
<li><a href="#bugs">BUGS</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlvar - Perl predefined variables</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>
</p>
<h2><a name="predefined_names">Predefined Names</a></h2>
<p>The following names have special meaning to Perl. Most
punctuation names have reasonable mnemonics, or analogs in the
shells. Nevertheless, if you wish to use long variable names,
you need only say</p>
<pre>
<span class="keyword">use</span> <span class="variable">English</span><span class="operator">;</span>
</pre>
<p>at the top of your program. This aliases all the short names to the long
names in the current package. Some even have medium names, generally
borrowed from <strong>awk</strong>. In general, it's best to use the</p>
<pre>
<span class="keyword">use</span> <span class="variable">English</span> <span class="string">'-no_match_vars'</span><span class="operator">;</span>
</pre>
<p>invocation if you don't need $PREMATCH, $MATCH, or $POSTMATCH, as it avoids
a certain performance hit with the use of regular expressions. See
<a href="../../lib/English.html">the English manpage</a>.</p>
<p>Variables that depend on the currently selected filehandle may be set by
calling an appropriate object method on the IO::Handle object, although
this is less efficient than using the regular built-in variables. (Summary
lines below for this contain the word HANDLE.) First you must say</p>
<pre>
<span class="keyword">use</span> <span class="variable">IO::Handle</span><span class="operator">;</span>
</pre>
<p>after which you may use either</p>
<pre>
method HANDLE EXPR</pre>
<p>or more safely,</p>
<pre>
HANDLE->method(EXPR)</pre>
<p>Each method returns the old value of the IO::Handle attribute.
The methods each take an optional EXPR, which, if supplied, specifies the
new value for the IO::Handle attribute in question. If not supplied,
most methods do nothing to the current value--except for
autoflush(), which will assume a 1 for you, just to be different.</p>
<p>Because loading in the IO::Handle class is an expensive operation, you should
learn how to use the regular built-in variables.</p>
<p>A few of these variables are considered "read-only". This means that if
you try to assign to this variable, either directly or indirectly through
a reference, you'll raise a run-time exception.</p>
<p>You should be very careful when modifying the default values of most
special variables described in this document. In most cases you want
to localize these variables before changing them, since if you don't,
the change may affect other modules which rely on the default values
of the special variables that you have changed. This is one of the
correct ways to read the whole file at once:</p>
<pre>
<span class="keyword">open</span> <span class="keyword">my</span> <span class="variable">$fh</span><span class="operator">,</span> <span class="string">"foo"</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="variable">$!</span><span class="operator">;</span>
<span class="keyword">local</span> <span class="variable">$/</span><span class="operator">;</span> <span class="comment"># enable localized slurp mode</span>
<span class="keyword">my</span> <span class="variable">$content</span> <span class="operator">=</span> <span class="operator"><</span><span class="variable">$fh</span><span class="operator">>;</span>
<span class="keyword">close</span> <span class="variable">$fh</span><span class="operator">;</span>
</pre>
<p>But the following code is quite bad:</p>
<pre>
<span class="keyword">open</span> <span class="keyword">my</span> <span class="variable">$fh</span><span class="operator">,</span> <span class="string">"foo"</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="variable">$!</span><span class="operator">;</span>
<span class="keyword">undef</span> <span class="variable">$/</span><span class="operator">;</span> <span class="comment"># enable slurp mode</span>
<span class="keyword">my</span> <span class="variable">$content</span> <span class="operator">=</span> <span class="operator"><</span><span class="variable">$fh</span><span class="operator">>;</span>
<span class="keyword">close</span> <span class="variable">$fh</span><span class="operator">;</span>
</pre>
<p>since some other module, may want to read data from some file in the
default "line mode", so if the code we have just presented has been
executed, the global value of <a href="#item___"><code>$/</code></a> is now changed for any other code
running inside the same Perl interpreter.</p>
<p>Usually when a variable is localized you want to make sure that this
change affects the shortest scope possible. So unless you are already
inside some short <code>{}</code> block, you should create one yourself. For
example:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$content</span> <span class="operator">=</span> <span class="string">''</span><span class="operator">;</span>
<span class="keyword">open</span> <span class="keyword">my</span> <span class="variable">$fh</span><span class="operator">,</span> <span class="string">"foo"</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="variable">$!</span><span class="operator">;</span>
<span class="operator">{</span>
<span class="keyword">local</span> <span class="variable">$/</span><span class="operator">;</span>
<span class="variable">$content</span> <span class="operator">=</span> <span class="operator"><</span><span class="variable">$fh</span><span class="operator">>;</span>
<span class="operator">}</span>
<span class="keyword">close</span> <span class="variable">$fh</span><span class="operator">;</span>
</pre>
<p>Here is an example of how your own code can go broken:</p>
<pre>
<span class="keyword">for</span> <span class="operator">(</span><span class="number">1</span><span class="operator">..</span><span class="number">5</span><span class="operator">){</span>
<span class="variable">nasty_break</span><span class="operator">();</span>
<span class="keyword">print</span> <span class="string">"$_ "</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="keyword">sub</span><span class="variable"> nasty_break </span><span class="operator">{</span>
<span class="variable">$_</span> <span class="operator">=</span> <span class="number">5</span><span class="operator">;</span>
<span class="comment"># do something with $_</span>
<span class="operator">}</span>
</pre>
<p>You probably expect this code to print:</p>
<pre>
1 2 3 4 5</pre>
<p>but instead you get:</p>
<pre>
5 5 5 5 5</pre>
<p>Why? Because <code>nasty_break()</code> modifies <a href="#item___"><code>$_</code></a> without localizing it
first. The fix is to add local():</p>
<pre>
<span class="keyword">local</span> <span class="variable">$_</span> <span class="operator">=</span> <span class="number">5</span><span class="operator">;</span>
</pre>
<p>It's easy to notice the problem in such a short example, but in more
complicated code you are looking for trouble if you don't localize
changes to the special variables.</p>
<p>The following list is ordered by scalar variables first, then the
arrays, then the hashes.</p>
<dl>
<dt><strong><a name="item__arg">$ARG</a></strong>
<dt><strong><a name="item___">$_</a></strong>
<dd>
<p>The default input and pattern-searching space. The following pairs are
equivalent:</p>
</dd>
<dd>
<pre>
<span class="keyword">while</span> <span class="operator">(<>)</span> <span class="operator">{...}</span> <span class="comment"># equivalent only in while!</span>
<span class="keyword">while</span> <span class="operator">(</span><span class="keyword">defined</span><span class="operator">(</span><span class="variable">$_</span> <span class="operator">=</span> <span class="operator"><>))</span> <span class="operator">{...}</span>
</pre>
</dd>
<dd>
<pre>
/^Subject:/
$_ =~ /^Subject:/</pre>
</dd>
<dd>
<pre>
tr/a-z/A-Z/
$_ =~ tr/a-z/A-Z/</pre>
</dd>
<dd>
<pre>
chomp
chomp($_)</pre>
</dd>
<dd>
<p>Here are the places where Perl will assume $_ even if you
don't use it:</p>
</dd>
<ul>
<li>
<p>Various unary functions, including functions like <a href="../../lib/Pod/perlfunc.html#item_ord"><code>ord()</code></a> and int(), as well
as the all file tests (<a href="../../lib/Pod/perlrun.html#item__2df"><code>-f</code></a>, <a href="../../lib/Pod/perlrun.html#item__2dd"><code>-d</code></a>) except for <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-t</code></a>, which defaults to
STDIN.</p>
</li>
<li>
<p>Various list functions like <a href="../../lib/Pod/perlfunc.html#item_print"><code>print()</code></a> and unlink().</p>
</li>
<li>
<p>The pattern matching operations <a href="../../lib/Pod/perlfunc.html#item_m_"><code>m//</code></a>, <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///</code></a>, and <a href="../../lib/Pod/perlfunc.html#item_tr_"><code>tr///</code></a> when used
without an <code>=~</code> operator.</p>
</li>
<li>
<p>The default iterator variable in a <code>foreach</code> loop if no other
variable is supplied.</p>
</li>
<li>
<p>The implicit iterator variable in the <a href="../../lib/Pod/perlfunc.html#item_grep"><code>grep()</code></a> and <a href="../../lib/Pod/perlfunc.html#item_map"><code>map()</code></a> functions.</p>
</li>
<li>
<p>The default place to put an input record when a <code><FH></code>
operation's result is tested by itself as the sole criterion of a <code>while</code>
test. Outside a <code>while</code> test, this will not happen.</p>
</li>
</ul>
<p>(Mnemonic: underline is understood in certain operations.)</p>
</dl>
<dl>
<dt><strong><a name="item__a">$a</a></strong>
<dt><strong><a name="item__b">$b</a></strong>
<dd>
<p>Special package variables when using sort(), see <a href="../../lib/Pod/perlfunc.html#sort">sort in the perlfunc manpage</a>.
Because of this specialness $a and $b don't need to be declared
(using use vars, or <a href="../../lib/Pod/perlfunc.html#item_our"><code>our())</code></a> even when using the <code>strict 'vars'</code> pragma.
Don't lexicalize them with <code>my $a</code> or <code>my $b</code> if you want to be
able to use them in the <a href="../../lib/Pod/perlfunc.html#item_sort"><code>sort()</code></a> comparison block or function.</p>
</dd>
</li>
</dl>
<dl>
<dt><strong><a name="item___digits_">$<<em>digits</em>></a></strong>
<dd>
<p>Contains the subpattern from the corresponding set of capturing
parentheses from the last pattern match, not counting patterns
matched in nested blocks that have been exited already. (Mnemonic:
like \digits.) These variables are all read-only and dynamically
scoped to the current BLOCK.</p>
</dd>
</li>
<dt><strong><a name="item__match">$MATCH</a></strong>
<dt><strong><a name="item___">$&</a></strong>
<dd>
<p>The string matched by the last successful pattern match (not counting
any matches hidden within a BLOCK or <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a> enclosed by the current
BLOCK). (Mnemonic: like & in some editors.) This variable is read-only
and dynamically scoped to the current BLOCK.</p>
</dd>
<dd>
<p>The use of this variable anywhere in a program imposes a considerable
performance penalty on all regular expression matches. See <a href="#bugs">BUGS</a>.</p>
</dd>
</li>
<dt><strong><a name="item__prematch">$PREMATCH</a></strong>
<dt><strong><a name="item___">$`</a></strong>
<dd>
<p>The string preceding whatever was matched by the last successful
pattern match (not counting any matches hidden within a BLOCK or eval
enclosed by the current BLOCK). (Mnemonic: <code>`</code> often precedes a quoted
string.) This variable is read-only.</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?