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

📄 ch28_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<pre class="programlisting">{    local $/;    $_ = &lt;FH&gt;;}</pre></blockquote><p>Setting <tt class="literal">$/</tt> to a reference to either an integer, a scalar containingan integer, or a scalar that's convertible to an integer will make<tt class="literal">readline</tt> and <tt class="literal">&lt;FH&gt;</tt> operations read in fixed-length records(with the maximum record size being the referenced integer) instead ofvariable-length record terminated by a particular string.  So this:<blockquote><pre class="programlisting">$/ = \32768; # or \"32768" or \$scalar_var_containing_32768open(FILE, $myfile);$record = &lt;FILE&gt;;</pre></blockquote>will read a record of no more than 32,768 bytes from the<tt class="literal">FILE</tt> handle.  If you're not reading from arecord-oriented file (or your operating system doesn't haverecord-oriented files), then you'll likely get a full chunk of datawith every read.  If a record is larger than the record size you'veset, you'll get the record back in pieces.  Record mode mixes wellwith line mode only on systems where standard I/O supplies a<em class="emphasis">read</em>(3) function; VMS is a notableexception.</p><p>Calling <tt class="literal">chomp</tt> when <tt class="literal">$/</tt> is set toenable record mode--or when it is undefined--has no effect.  See alsothe <span class="option">-0</span> (the digit) and the <span class="option">-l</span> (theletter) command-line switches in <a href="ch19_01.htm">Chapter 19, "The Command-Line Interface"</a>.  (Mnemonic:<tt class="literal">/</tt> is used to separate lines when quoting poetry.)</p></dd><dt><b><tt class="literal">@ISA</tt></b></dt><dd><p><a name="INDEX-4508"></a>[PKG] This array contains names of other packages to lookthrough when a method call cannot be found in the current package.  That is,it contains the base classes of the package.  The <tt class="literal">use base</tt>pragma sets this implicitly.  It is not exempt from <tt class="literal">use strict</tt>.  See <a href="ch12_01.htm">Chapter 12, "Objects"</a>.</p></dd><dt><b><tt class="literal">@LAST_MATCH_END</tt></b></dt><dt><b><tt class="literal">@+</tt></b></dt><dd><p><a name="INDEX-4509"></a><a name="INDEX-4510"></a><a name="INDEX-4511"></a>This array holds the offsets of the ends of the last successfulsubmatches in the currently active dynamic scope.<tt class="literal">$+[0]</tt> is the offset of the end of the entire match.This is the same value the <tt class="literal">pos</tt> function returnswhen called on the variable that was matched against.  (When we say"offset of the end", we really mean the offset to the first character<em class="emphasis">following</em> the end of whatever matched, so that wecan subtract beginning offsets from end offsets and arrive at thelength.)  The <em class="emphasis">n</em>th element of this arrayholds the offset of the <em class="emphasis">n</em>th submatch, so<tt class="literal">$+[1]</tt> is the offset where <tt class="literal">$1</tt>ends, <tt class="literal">$+[2]</tt> the offset where <tt class="literal">$2</tt>ends, and so on.  You can use <tt class="literal">$#+</tt> to determine howmany subgroups were in the last successful match.  See also<tt class="literal">@_</tt> (<tt class="literal">@LAST_MATCH_START</tt>).</p><p>After a successful match against some variable <tt class="literal">$var</tt>:</p></dd><ul><li><tt>$`</tt> is the same as <tt>substr($var, 0, $-[0])</tt><li><tt>$&amp;</tt> is the same as <tt>substr($var, $-[0], $+[0] - $-[0])</tt><li><tt>$'</tt> is the same as <tt>substr($var, $+[0])</tt><li><tt>$1</tt> is the same as <tt>substr($var, $-[1], $+[1] - $-[1])</tt><li><tt>$2</tt> is the same as <tt>substr($var, $-[2], $+[2] - $-[2])</tt><li><tt>$3</tt> is the same as <tt>substr($var, $-[3], $+[3] - $-[2])</tt>, and so on.</ul><dt><b><tt class="literal">@LAST_MATCH_START</tt></b></dt><dt><b><tt class="literal">@-</tt></b></dt><dd><p><a name="INDEX-4512"></a><a name="INDEX-4513"></a><a name="INDEX-4514"></a>[DYN,RO] This array holds the offsets of the beginnings of the lastsuccessful submatches in the currently active dynamic scope.<tt class="literal">$-[0]</tt> is the offset of the beginning of the entirematch.  The <em class="emphasis">n</em>th element of this arrayholds the offset of the <em class="emphasis">n</em>th submatch, so<tt class="literal">$-[1]</tt> is the offset where <tt class="literal">$1</tt>begins, <tt class="literal">$-[2]</tt> the offset where<tt class="literal">$2</tt> begins, and so on.  You can use<tt class="literal">$#-</tt> to determine how many subgroups were in thelast successful match.  See also <tt class="literal">@+</tt>(<tt class="literal">@LAST_MATCH_END</tt>).</p></dd><dt><b><tt class="literal">$LAST_PAREN_MATCH</tt></b></dt><dt><b><tt class="literal">$+</tt></b></dt><dd><p><a name="INDEX-4515"></a><a name="INDEX-4516"></a>[DYN,RO] This returns the last parenthesized submatch from the lastsuccessful pattern in the currently active dynamic scope.  Thisis useful when you don't know (or care) which of a set of alternativepatterns matched.  (Mnemonic: be positive and forward looking.)Example:<blockquote><pre class="programlisting">$rev = $+   if /Version: (.*)|Revision: (.*)/;</pre></blockquote></p></dd><dt><b><tt class="literal">$LAST_REGEXP_CODE_RESULT</tt></b></dt><dt><b><tt class="literal">$^R</tt></b></dt><dd><p>[DYN] This contains the result of the last snippet of code executedinside a successful pattern with the <tt class="literal">(?{&nbsp;</tt><em class="replaceable">CODE</em><tt class="literal">&nbsp;})</tt> construct.<tt class="literal">$^R</tt> gives you a way to execute code and remember the result for uselater in the pattern, or even afterward.</p><p>As the Perl regular expression engine moves through the pattern, itmay encounter multiple <tt class="literal">(?{&nbsp;</tt><em class="replaceable">CODE</em><tt class="literal">&nbsp;})</tt> expressions.  As it does, itremembers each value of <tt class="literal">$^R</tt> so that if it later has to backtrackpast an expression, it restores the previous value of <tt class="literal">$^R</tt>.  Inother words, <tt class="literal">$^R</tt> has a dynamic scope within the pattern, much like<tt class="literal">$1</tt> and friends.</p><p>So <tt class="literal">$^R</tt> is not simply the result of the lastsnippet of code executed inside a pattern.  It's the result of thelast snippet of code <em class="emphasis">leading to a successful match</em>.  A corollary isthat if the match was not successful, <tt class="literal">$^R</tt> will be restored towhatever value it had before the match occurred.</p><p>If the <tt class="literal">(?{&nbsp;</tt><em class="replaceable">CODE</em><tt class="literal">&nbsp;})</tt> pattern is functioning directlyas the conditional of a <tt class="literal">(?(</tt><em class="replaceable">COND</em><tt class="literal">)</tt><em class="replaceable">IFTRUE</em><tt class="literal">|</tt><em class="replaceable">IFFALSE</em><tt class="literal">)</tt>subpattern, <tt class="literal">$^R</tt> is not set.</p></dd><dt><b><tt class="literal">$LIST_SEPARATOR</tt></b></dt><dt><b><tt class="literal">$"</tt></b></dt><dd><p><a name="INDEX-4517"></a><a name="INDEX-4518"></a>[ALL] When an array or slice is interpolated into a double-quotedstring (or the like), this variable specifies the string to putbetween individual elements.  Default is a space.  (Mnemonic: obvious,one hopes.)</p></dd><dt><b><tt class="literal">$^M</tt></b></dt><dd><p><a name="INDEX-4519"></a><a name="INDEX-4520"></a><a name="INDEX-4521"></a><a name="INDEX-4522"></a><a name="INDEX-4523"></a><a name="INDEX-4524"></a>[ALL] By default, running out of memory is not trappable.  However, if your<em class="emphasis">perl</em> was compiled to take advantage of <tt class="literal">$^M</tt>, you may use it as anemergency memory pool.  If your Perl is compiled with<tt class="userinput"><b>-DPERL_EMERGENCY_SBRK</b></tt> and uses Perl's <tt class="literal">malloc</tt>, then:<blockquote><pre class="programlisting">$^M = 'a' x (1 &lt;&lt; 16);</pre></blockquote>would allocate a 64K buffer for emergency use.  See the <em class="emphasis">INSTALL</em> filein the Perl source distribution directory for information on how toenable this option.  As a disincentive to casual use of this advancedfeature, there is no <tt class="literal">use English</tt> long name for this variable (andwe won't tell you what the mnemonic is).</p></dd><dt><b><tt class="literal">$MATCH</tt></b></dt><dt><b><tt class="literal">$&amp;</tt></b></dt><dd><p><a name="INDEX-4525"></a><a name="INDEX-4526"></a>[DYN,RO] The string matched by the last successful pattern match inthe currently active dynamic scope.  (Mnemonic: like<tt class="literal">&amp;</tt> in some editors.)</p></dd><dt><b><tt class="literal">$OLD_PERL_VERSION</tt></b></dt><dt><b><tt class="literal">$]</tt></b></dt><dd><p><a name="INDEX-4527"></a><a name="INDEX-4528"></a><a name="INDEX-4529"></a>[ALL] Returns the version + patchlevel/1000.  It can be used todetermine at the beginning of a script whether the Perl interpreterexecuting the script is in the right range of versions.  (Mnemonic: isthis version of Perl in the right bracket?) Example:<blockquote><pre class="programlisting">warn "No checksumming!\n" if $] &lt; 3.019;die "Must have prototyping available\n" if $] &lt; 5.003;</pre></blockquote>See also the documentation of <tt class="literal">use</tt><em class="replaceable">VERSION</em> and <tt class="literal">require</tt><em class="replaceable">VERSION</em> for a convenient way to fail if thePerl interpreter is too old.  See <tt class="literal">$^V</tt> for a moreflexible UTF-8 representation of the Perl version.</p></dd><dt><b><tt class="literal">$OSNAME</tt></b></dt><dt><b><tt class="literal">$^O</tt></b></dt><dd><p><a name="INDEX-4530"></a><a name="INDEX-4531"></a><a name="INDEX-4532"></a><a name="INDEX-4533"></a>[ALL] This variable contains the name of the platform (usually the operatingsystem) the current <em class="emphasis">perl</em> binary was compiled for.  It's a cheapalternative to pulling it out of the <tt class="literal">Config</tt> module.</p></dd><dt><b><tt class="literal">$OS_ERROR</tt></b></dt><dt><b><tt class="literal">$ERRNO</tt></b></dt><dt><b><tt class="literal">$!</tt></b></dt><dd><p><a name="INDEX-4534"></a><a name="INDEX-4535"></a><a name="INDEX-4536"></a><a name="INDEX-4537"></a>[ALL] If used in a numeric context, yields the current value of the lastsyscall error, with all the usual caveats.  (This means that youshouldn't depend on the value of <tt class="literal">$!</tt> to be anything in particularunless you've gotten a specific error return indicating a systemerror.)  If used in a string context, <tt class="literal">$!</tt> yields the corresponding systemerror string.  You can assign an error number to <tt class="literal">$!</tt> if, for instance, you want<tt class="literal">$!</tt> to return the string for that particular error, or you want to setthe exit value for <tt class="literal">die</tt>.  See also the <tt class="literal">Errno</tt> module in <a href="ch32_01.htm">Chapter 32, "Standard Modules"</a>.  (Mnemonic: what just went bang?)</p></dd><dt><b><tt class="literal">%OS_ERROR</tt></b></dt><dt><b><tt class="literal">%ERRNO</tt></b></dt><dt><b><tt class="literal">%!</tt></b></dt><dd><p>[ALL] This hash is defined only if you've loaded the standard<tt class="literal">Errno</tt> module described in <a href="ch32_01.htm">Chapter 32, "Standard Modules"</a>.  Once you've done this, youcan subscript into <tt class="literal">%!</tt> using a particular errorstring, and its value is true only if that's the current error.  Forexample, <tt class="literal">$!{ENOENT}</tt> is true only if the C<tt class="literal">errno</tt> variable is currently set to the C<tt class="literal">#define</tt> value, <tt class="literal">ENOENT</tt>.  This isconvenient for accessing vendor-specific symbols.</p></dd><dt><b><tt class="literal">autoflush</tt> <em class="replaceable">HANDLE EXPR</em></b></dt><dt><b><tt class="literal">$OUTPUT_AUTOFLUSH</tt></b></dt><dt><b><tt class="literal">$AUTOFLUSH</tt></b></dt><dt><b><tt class="literal">$|</tt></b></dt><dd><p><a name="INDEX-4538"></a><a name="INDEX-4539"></a><a name="INDEX-4540"></a><a name="INDEX-4541"></a><a name="INDEX-4542"></a>[FHA] If set to true, forces a buffer flush after every <tt class="literal">print</tt>,<tt class="literal">printf</tt>, and <tt class="literal">write</tt> on the currently selected output handle.(We call this <em class="emphasis">command buffering</em>.  Contrary to popular belief, settingthis variable does not turn off buffering.)  The default is false, which onmany systems means that <tt class="literal">STDOUT</tt> will be line buffered ifoutput is to the terminal, and block buffered otherwise, even on pipesand sockets.  Setting this variable is useful when you areoutputting to a pipe, such as when you are running a Perl script under<em class="emphasis">rsh</em>(1) and want to see the output as it's happening.  If you havepending, unflushed data in the currently selected filehandle's outputbuffer when this variable is set to true, that buffer will be immediatelyflushed as a side-effect of assignment.   See the one-argument form of<tt class="literal">select</tt> for examples of controlling buffering on filehandles otherthan <tt class="literal">STDOUT</tt>.  (Mnemonic: when you want your pipes to be piping hot.)</p><p>This variable has no effect on input buffering; for that, see <tt class="literal">getc</tt>in <a href="ch29_01.htm">Chapter 29, "Functions"</a> or the example in the <tt class="literal">POSIX</tt> modulein <a href="ch32_01.htm">Chapter 32, "Standard Modules"</a>.</p></dd><dt><b><tt class="literal">$OUTPUT_FIELD_SEPARATOR</tt></b></dt><dt><b><tt class="literal">$OFS</tt></b></dt><dt><b><tt class="literal">$,</tt></b></dt><dd><p><a name="INDEX-4543"></a><a name="INDEX-4544"></a><a name="INDEX-4545"></a><a name="INDEX-4546"></a><a name="INDEX-4547"></a><a name="INDEX-4548"></a><a name="INDEX-4549"></a>[ALL] The output field separator (terminator, actually) for <tt class="literal">print</tt>.  Ordinarily, <tt class="literal">print</tt>simply prints out the list elements you specify without anythingbetween them.  Set this variable as you would set <em class="emphasis">awk</em>'s<tt class="literal">OFS</tt> variable to specify what is printed between fields.  (Mnemonic:what is printed when there is a "<tt class="literal">,</tt>" in your <tt class="literal">print</tt> statement.)</p></dd><dt><b><tt class="literal">$OUTPUT_RECORD_SEPARATOR</tt></b></dt>

⌨️ 快捷键说明

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