perlvar.html

来自「perl教程」· HTML 代码 · 共 1,313 行 · 第 1/5 页

HTML
1,313 行
字号
</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__postmatch">$POSTMATCH</a></strong>

<dt><strong><a name="item___">$'</a></strong>

<dd>
<p>The string following whatever was 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: <code>'</code> often follows a quoted
string.)  Example:</p>
</dd>
<dd>
<pre>
    <span class="keyword">local</span> <span class="variable">$_</span> <span class="operator">=</span> <span class="string">'abcdefghi'</span><span class="operator">;</span>
    <span class="regex">/def/</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="string">"$`:$&amp;:$'\n"</span><span class="operator">;</span>         <span class="comment"># prints abc:def:ghi</span>
</pre>
</dd>
<dd>
<p>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__last_paren_match">$LAST_PAREN_MATCH</a></strong>

<dt><strong><a name="item___">$+</a></strong>

<dd>
<p>The text matched by the last bracket of the last successful search pattern.
This is useful if you don't know which one of a set of alternative patterns
matched. For example:</p>
</dd>
<dd>
<pre>
    <span class="regex">/Version: (.*)|Revision: (.*)/</span> <span class="operator">&amp;&amp;</span> <span class="operator">(</span><span class="variable">$rev</span> <span class="operator">=</span> <span class="variable">$+</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>(Mnemonic: be positive and forward looking.)
This variable is read-only and dynamically scoped to the current BLOCK.</p>
</dd>
</li>
<dt><strong><a name="item___n">$^N</a></strong>

<dd>
<p>The text matched by the used group most-recently closed (i.e. the group
with the rightmost closing parenthesis) of the last successful search
pattern.  (Mnemonic: the (possibly) Nested parenthesis that most
recently closed.)</p>
</dd>
<dd>
<p>This is primarily used inside <code>(?{...})</code> blocks for examining text
recently matched. For example, to effectively capture text to a variable
(in addition to <code>$1</code>, <code>$2</code>, etc.), replace <code>(...)</code> with</p>
</dd>
<dd>
<pre>
     <span class="operator">(</span><span class="regex">?:(...)(?</span><span class="operator">{</span> <span class="variable">$var</span> <span class="operator">=</span> <span class="variable">$^N</span> <span class="operator">}))</span>
</pre>
</dd>
<dd>
<p>By setting and then using <code>$var</code> in this way relieves you from having to
worry about exactly which numbered set of parentheses they are.</p>
</dd>
<dd>
<p>This variable is dynamically scoped to the current BLOCK.</p>
</dd>
</li>
<dt><strong><a name="item__last_match_end">@LAST_MATCH_END</a></strong>

<dt><strong><a name="item___">@+</a></strong>

<dd>
<p>This array holds the offsets of the ends of the last successful
submatches in the currently active dynamic scope.  <code>$+[0]</code> is
the offset into the string of the end of the entire match.  This
is the same value as what the <a href="../../lib/Pod/perlfunc.html#item_pos"><code>pos</code></a> function returns when called
on the variable that was matched against.  The <em>n</em>th element
of this array holds the offset of the <em>n</em>th submatch, so
<code>$+[1]</code> is the offset past where $1 ends, <code>$+[2]</code> the offset
past where $2 ends, and so on.  You can use <code>$#+</code> to determine
how many subgroups were in the last successful match.  See the
examples given for the <a href="#item__"><code>@-</code></a> variable.</p>
</dd>
</li>
<dt><strong><a name="item___">$*</a></strong>

<dd>
<p>Set to a non-zero integer value to do multi-line matching within a
string, 0 (or undefined) to tell Perl that it can assume that strings
contain a single line, for the purpose of optimizing pattern matches.
Pattern matches on strings containing multiple newlines can produce
confusing results when <a href="#item___"><code>$*</code></a> is 0 or undefined. Default is undefined.
(Mnemonic: * matches multiple things.) This variable influences the
interpretation of only <code>^</code> and <code>$</code>. A literal newline can be searched
for even when <a href="#item___"><code>$* == 0</code></a>.</p>
</dd>
<dd>
<p>Use of <a href="#item___"><code>$*</code></a> is deprecated in modern Perl, supplanted by 
the <code>/s</code> and <code>/m</code> modifiers on pattern matching.</p>
</dd>
<dd>
<p>Assigning a non-numerical value to <a href="#item___"><code>$*</code></a> triggers a warning (and makes
<a href="#item___"><code>$*</code></a> act if <a href="#item___"><code>$* == 0</code></a>), while assigning a numerical value to <a href="#item___"><code>$*</code></a>
makes that an implicit <a href="../../lib/Pod/perlfunc.html#item_int"><code>int</code></a> is applied on the value.</p>
</dd>
</li>
<dt><strong><a name="item_input_line_number">HANDLE-&gt;<code>input_line_number(EXPR)</code></a></strong>

<dt><strong><a name="item__input_line_number">$INPUT_LINE_NUMBER</a></strong>

<dt><strong><a name="item__nr">$NR</a></strong>

<dt><strong><a name="item___">$.</a></strong>

<dd>
<p>Current line number for the last filehandle accessed.</p>
</dd>
<dd>
<p>Each filehandle in Perl counts the number of lines that have been read
from it.  (Depending on the value of <a href="#item___"><code>$/</code></a>, Perl's idea of what
constitutes a line may not match yours.)  When a line is read from a
filehandle (via <a href="../../lib/Pod/perlfunc.html#item_readline"><code>readline()</code></a> or <code>&lt;&gt;</code>), or when <a href="../../lib/Pod/perlfunc.html#item_tell"><code>tell()</code></a> or <a href="../../lib/Pod/perlfunc.html#item_seek"><code>seek()</code></a> is
called on it, <a href="#item___"><code>$.</code></a> becomes an alias to the line counter for that
filehandle.</p>
</dd>
<dd>
<p>You can adjust the counter by assigning to <a href="#item___"><code>$.</code></a>, but this will not
actually move the seek pointer.  <em>Localizing <a href="#item___"><code>$.</code></a> will not localize
the filehandle's line count</em>.  Instead, it will localize perl's notion
of which filehandle <a href="#item___"><code>$.</code></a> is currently aliased to.</p>
</dd>
<dd>
<p><a href="#item___"><code>$.</code></a> is reset when the filehandle is closed, but <strong>not</strong> when an open
filehandle is reopened without an intervening close().  For more
details, see <a href="../../lib/Pod/perlop.html#ie_sol_o_operators">I&sol;O Operators in the perlop manpage</a>.  Because <code>&lt;&gt;</code> never does
an explicit close, line numbers increase across ARGV files (but see
examples in <a href="../../lib/Pod/perlfunc.html#eof">eof in the perlfunc manpage</a>).</p>
</dd>
<dd>
<p>You can also use <a href="#item_input_line_number"><code>HANDLE-&gt;input_line_number(EXPR)</code></a> to access the
line counter for a given filehandle without having to worry about
which handle you last accessed.</p>
</dd>
<dd>
<p>(Mnemonic: many programs use &quot;.&quot; to mean the current line number.)</p>
</dd>
</li>
<dt><strong><a name="item_input_record_separator">IO::Handle-&gt;<code>input_record_separator(EXPR)</code></a></strong>

<dt><strong><a name="item__input_record_separator">$INPUT_RECORD_SEPARATOR</a></strong>

<dt><strong><a name="item__rs">$RS</a></strong>

<dt><strong><a name="item___">$/</a></strong>

<dd>
<p>The input record separator, newline by default.  This 
influences Perl's idea of what a &quot;line&quot; is.  Works like <strong>awk</strong>'s RS
variable, including treating empty lines as a terminator if set to
the null string.  (An empty line cannot contain any spaces
or tabs.)  You may set it to a multi-character string to match a
multi-character terminator, or to <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> to read through the end
of file.  Setting it to <code>&quot;\n\n&quot;</code> means something slightly
different than setting to <code>&quot;&quot;</code>, if the file contains consecutive
empty lines.  Setting to <code>&quot;&quot;</code> will treat two or more consecutive
empty lines as a single empty line.  Setting to <code>&quot;\n\n&quot;</code> will
blindly assume that the next input character belongs to the next
paragraph, even if it's a newline.  (Mnemonic: / delimits
line boundaries when quoting poetry.)</p>
</dd>
<dd>
<pre>
    <span class="keyword">local</span> <span class="variable">$/</span><span class="operator">;</span>           <span class="comment"># enable "slurp" mode</span>
    <span class="keyword">local</span> <span class="variable">$_</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">FH</span><span class="operator">&gt;;</span>    <span class="comment"># whole file now here</span>
    <span class="regex">s/\n[ \t]+/ /g</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Remember: the value of <a href="#item___"><code>$/</code></a> is a string, not a regex.  <strong>awk</strong> has to be
better for something. :-)</p>
</dd>
<dd>
<p>Setting <a href="#item___"><code>$/</code></a> to a reference to an integer, scalar containing an integer, or
scalar that's convertible to an integer will attempt to read records
instead of lines, with the maximum record size being the referenced
integer.  So this:</p>
</dd>
<dd>
<pre>
    <span class="keyword">local</span> <span class="variable">$/</span> <span class="operator">=</span> <span class="operator">\</span><span class="number">32768</span><span class="operator">;</span> <span class="comment"># or \"32768", or \$var_containing_32768</span>
    <span class="keyword">open</span> <span class="keyword">my</span> <span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$myfile</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="operator">&lt;</span><span class="variable">$fh</span><span class="operator">&gt;;</span>
</pre>
</dd>
<dd>
<p>will read a record of no more than 32768 bytes from FILE.  If you're
not reading from a record-oriented file (or your OS doesn't have
record-oriented files), then you'll likely get a full chunk of data
with every read.  If a record is larger than the record size you've
set, you'll get the record back in pieces.</p>
</dd>
<dd>
<p>On VMS, record reads are done with the equivalent of <a href="../../lib/Pod/perlfunc.html#item_sysread"><code>sysread</code></a>,
so it's best not to mix record and non-record reads on the same
file.  (This is unlikely to be a problem, because any file you'd
want to read in record mode is probably unusable in line mode.)
Non-VMS systems do normal I/O, so it's safe to mix record and
non-record reads of a file.</p>
</dd>
<dd>
<p>See also <a href="../../lib/Pod/perlport.html#newlines">Newlines in the perlport manpage</a>.  Also see <a href="#item___"><code>$.</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_autoflush">HANDLE-&gt;<code>autoflush(EXPR)</code></a></strong>

<dt><strong><a name="item__output_autoflush">$OUTPUT_AUTOFLUSH</a></strong>

<dt><strong><a name="item___">$|</a></strong>

<dd>
<p>If set to nonzero, forces a flush right away and after every write
or print on the currently selected output channel.  Default is 0
(regardless of whether the channel is really buffered by the
system or not; <a href="#item___"><code>$|</code></a> tells you only whether you've asked Perl
explicitly to flush after each write).  STDOUT will
typically be line buffered if output is to the terminal and block
buffered otherwise.  Setting this variable is useful primarily when
you are outputting to a pipe or socket, such as when you are running
a Perl program under <strong>rsh</strong> and want to see the output as it's
happening.  This has no effect on input buffering.  See <a href="../../lib/Pod/perlfunc.html#getc">getc in the perlfunc manpage</a>
for that.  (Mnemonic: when you want your pipes to be piping hot.)</p>
</dd>
</li>
<dt><strong><a name="item_output_field_separator">IO::Handle-&gt;output_field_separator EXPR</a></strong>

<dt><strong><a name="item__output_field_separator">$OUTPUT_FIELD_SEPARATOR</a></strong>

<dt><strong><a name="item__ofs">$OFS</a></strong>

<dt><strong><a name="item___">$,</a></strong>

<dd>
<p>The output field separator for the print operator.  If defined, this
value is printed between each of print's arguments.  Default is <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.
(Mnemonic: what is printed when there is a &quot;,&quot; in your print statement.)</p>
</dd>
</li>
<dt><strong><a name="item_output_record_separator">IO::Handle-&gt;output_record_separator EXPR</a></strong>

<dt><strong><a name="item__output_record_separator">$OUTPUT_RECORD_SEPARATOR</a></strong>

<dt><strong><a name="item__ors">$ORS</a></strong>

<dt><strong><a name="item___">$\</a></strong>

⌨️ 快捷键说明

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