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

📄 ch28_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<dt><b><tt class="literal">$ORS</tt></b></dt><dt><b><tt class="literal">$\</tt></b></dt><dd><p><a name="INDEX-4550"></a><a name="INDEX-4551"></a>[ALL] The output record separator (terminator, actually) for<tt class="literal">print</tt>.  Ordinarily, <tt class="literal">print</tt> simplyprints out the comma-separated fields you specify, with no trailingnewline or record separator assumed.  Set this variable as you wouldset <em class="emphasis">awk</em>'s <tt class="literal">ORS</tt> variableto specify what is printed at the end of the <tt class="literal">print</tt>.(Mnemonic: you set <tt class="literal">$\</tt> instead of adding<tt class="literal">"\n"</tt> at the end of the print.  Also, it's just like<tt class="literal">/</tt>, but it's what you get "back" from Perl.) Seealso the <span class="option">-l</span> (for "line") command-line switch in <a href="ch19_01.htm">Chapter 19, "The Command-Line Interface"</a>.</p></dd><dt><b><tt class="literal">%OVERLOAD</tt></b></dt><dd><p>[NOT,PKG] This hash's entries are set internally by the <tt class="literal">use overload</tt> pragmato implement operator overloading for objects of the currentpackage's class.  See <a href="ch13_01.htm">Chapter 13, "Overloading"</a>.</p></dd><dt><b><tt class="literal">$PERLDB</tt></b></dt><dt><b><tt class="literal">$^P</tt></b></dt><dd><p><a name="INDEX-4552"></a><a name="INDEX-4553"></a></p><p>[NOT,ALL] The internal variable for enabling the Perl debugger(<em class="emphasis">perl -d</em>).</p></dd><dt><b><tt class="literal">$PERL_VERSION</tt></b></dt><dt><b><tt class="literal">$^V</tt></b></dt><dd><p><a name="INDEX-4554"></a><a name="INDEX-4555"></a><a name="INDEX-4556"></a><a name="INDEX-4557"></a><a name="INDEX-4558"></a>[ALL] The revision, version, and subversion of the Perl interpreter,represented as a binary "version string".  V-strings don't generallyhave a a numeric value, but this variable is dual-valued, and has anumeric value equivalent to the old <tt class="literal">$]</tt> variable; that is, a floating-point number that amounts to revision + version/1000 +subversion/1,000,000.  The string value is made of UTF-8 characters: <tt class="literal">chr($revision) .  chr($version) .  chr($subversion)</tt>.This means that <tt class="literal">$^V</tt> is <em class="emphasis">not</em> printable.  Toprint it, you have to say:<blockquote><pre class="programlisting">printf "%vd", $^V;</pre></blockquote>On the plus side, it also means that ordinary string comparison can beused to determine whether the Perl interpreter executing your script is inthe right range of versions.  (This applies to any version numbersrepresented with v-strings, not just Perl's.)Example:<blockquote><pre class="programlisting">warn "No 'our' declarations!\n" if $^V lt v5.6;</pre></blockquote>See 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 therunning Perl interpreter is older than you were hoping.  See also<tt class="literal">$]</tt> for the original representation of the Perlversion.</p></dd><dt><b><tt class="literal">$POSTMATCH</tt></b></dt><dt><b><tt class="literal">$'</tt></b></dt><dd><p><a name="INDEX-4559"></a><a name="INDEX-4560"></a>[DYN,RO] The string following whatever was matched by the last successfulpattern in the currently active dynamic scope.  (Mnemonic: <tt class="literal">'</tt>often follows a quoted string.) Example:<blockquote><pre class="programlisting">$_ = 'abcdefghi';/def/;print "$`:$&amp;:$'\n";         # prints abc:def:ghi</pre></blockquote>Thanks to dynamic scope, Perl can't know which patterns will needtheir results saved away into these variables, so mentioning <tt class="literal">$`</tt> or <tt class="literal">$'</tt>anywhere in a program incurs a performance penalty on all patternmatches throughout the program.  This isn't much of an issue insmall programs, but you probably should avoid this pair when you'rewriting reusable module code.  The example above can be equivalentlyrecoded like this, but without the global performance hit:<blockquote><pre class="programlisting">$_ = 'abcdefghi';/(.*?)(def)(.*)/s;          # /s in case $1 contains newlinesprint "$1:$2:$3\n";         # prints abc:def:ghi</pre></blockquote></p></dd><dt><b><tt class="literal">$PREMATCH</tt></b></dt><dt><b><tt class="literal">$`</tt></b></dt><dd><p><a name="INDEX-4561"></a><a name="INDEX-4562"></a>[DYN,RO] The string preceding whatever was matched by the lastsuccessful pattern in the currently active dynamic scope.  (Mnemonic:<tt class="literal">`</tt> often precedes a quoted string.) See the performance note under<tt class="literal">$'</tt> previously.</p></dd><dt><b><tt class="literal">$PROCESS_ID</tt></b></dt><dt><b><tt class="literal">$PID</tt></b></dt><dt><b><tt class="literal">$$</tt></b></dt><dd><p><a name="INDEX-4563"></a><a name="INDEX-4564"></a><a name="INDEX-4565"></a>[ALL] The process number (PID) of the Perl running this script.  Thisvariable is automatically updated upon a <tt class="literal">fork</tt>.  Infact, you can even set <tt class="literal">$$</tt> yourself; this will not,however, change your PID.  That would be a neat trick.  (Mnemonic:same as in the various shells.)</p><p>You need to be careful not to use <tt class="literal">$$</tt> anywhere itmight be misinterpreted as a dereference:<tt class="literal">$$alphanum</tt>.  In this situation, write<tt class="literal">${$}alphanum</tt> to distinguish it from<tt class="literal">${$alphanum}</tt>.</p></dd><dt><b><tt class="literal">$PROGRAM_NAME</tt></b></dt><dt><b><tt class="literal">$0</tt></b></dt><dd><p><a name="INDEX-4566"></a><a name="INDEX-4567"></a><a name="INDEX-4568"></a><a name="INDEX-4569"></a><a name="INDEX-4570"></a>[ALL] Contains the name of the file containing the Perl script beingexecuted.  Assignment to <tt class="literal">$0</tt> is magical: it attempts to modify theargument area that the <em class="emphasis">ps</em>(1) program normally reports on.  Thisis more useful as a way of indicating the current program statethan it is for hiding the program you're running.  But it doesn'twork on all systems.  (Mnemonic: same as <em class="emphasis">sh</em>, <em class="emphasis">ksh</em>, <em class="emphasis">bash</em>, etc.)</p></dd><dt><b><tt class="literal">$REAL_GROUP_ID</tt></b></dt><dt><b><tt class="literal">$GID</tt></b></dt><dt><b><tt class="literal">$(</tt></b></dt><dd><p><a name="INDEX-4571"></a><a name="INDEX-4572"></a><a name="INDEX-4573"></a><a name="INDEX-4574"></a><a name="INDEX-4575"></a><a name="INDEX-4576"></a><a name="INDEX-4577"></a>[ALL] The real group ID (GID) of this process.  If you are on aplatform that supports simultaneous membership in multiple groups,<tt class="literal">$(</tt> gives aspace-separated  list of groups you are in.  The first number is the one returned by<em class="emphasis">getgid</em>(2), and the subsequent ones by<em class="emphasis">getgroups</em>(2), one of which may be the sameas the first number.</p><p>However, a value assigned to <tt class="literal">$(</tt> must be a singlenumber used to set the real GID.  So the value given by<tt class="literal">$(</tt> should <em class="emphasis">not</em> be assigned backto <tt class="literal">$(</tt> without being forced to be numeric, such asby adding zero.  This is because you can have only one real group.See <tt class="literal">$)</tt> (<tt class="literal">$EFFECTIVE_GROUP_ID</tt>)instead, which allows you to set multiple effective groups.</p><p>(Mnemonic: parentheses are used to <em class="emphasis">group</em> things.  The real GID is the group you <em class="emphasis">left</em>, if you're running setgid.)</p></dd><dt><b><tt class="literal">$REAL_USER_ID</tt></b></dt><dt><b><tt class="literal">$UID</tt></b></dt><dt><b><tt class="literal">$&lt;</tt></b></dt><dd><p><a name="INDEX-4578"></a><a name="INDEX-4579"></a>[ALL] The real user ID (UID) of this process as returned by the <em class="emphasis">getuid</em>(2) syscall.  Whether and how you canmodify this is subject to the vagaries of your system'simplementation--see examples under <tt class="literal">$&gt;</tt> (<tt class="literal">$EFFECTIVE_USER_ID</tt>).(Mnemonic: it's the UID you came <em class="emphasis">from</em>, if you're running setuid.)</p></dd><dt><b><tt class="literal">%SIG</tt></b></dt><dd><p><a name="INDEX-4580"></a><a name="INDEX-4581"></a></p><p>[ALL] The hash used to set signal handlers for various signals.(See the section "Signals" in <a href="ch16_01.htm">Chapter 16, "Interprocess Communication"</a>.)For example:<blockquote><pre class="programlisting">sub handler {    my $sig = shift;   # 1st argument is signal name    syswrite STDERR, "Caught a SIG$sig--shutting down\n";                       # Avoid standard I/O in async handlers to suppress                       # core dumpage.  (Even that string concat is risky.)    close LOG;         # This calls standard I/O, so may dump core anyway!    exit 1;            # But since we're exiting, no harm in trying.}$SIG{INT}  = \&amp;handler;$SIG{QUIT} = \&amp;handler;...$SIG{INT}  = 'DEFAULT';    # restore default action$SIG{QUIT} = 'IGNORE';     # ignore SIGQUIT</pre></blockquote>The <tt class="literal">%SIG</tt> hash contains undefined valuescorresponding to those signals for which no handler has been set. Ahandler may be specified as a subroutine reference or as a string.  Astring value that is not one of the two special actions"<tt class="literal">DEFAULT</tt>" or "<tt class="literal">IGNORE</tt>" is thename of a function, which, if unqualified by package, is interpretedto be the <tt class="literal">main</tt> package.  Here are some otherexamples:<blockquote><pre class="programlisting">$SIG{PIPE} = "Plumber";   # okay, assumes main::Plumber$SIG{PIPE} = \&amp;Plumber;   # fine, use Plumber from current package</pre></blockquote><a name="INDEX-4582"></a></p><p>Certain internal hooks can also be set using the <tt class="literal">%SIG</tt> hash.  Theroutine indicated by <tt class="literal">$SIG{__WARN__}</tt> is called when a warningmessage is about to be printed.  The warning message is passed asthe first argument.  The presence of a <tt class="literal">__WARN__</tt> hook causes theordinary printing of warnings to <tt class="literal">STDERR</tt> to be suppressed.  Youcan use this to save warnings in a variable or to turn warnings intofatal errors, like this:<blockquote><pre class="programlisting">local $SIG{__WARN__} = sub { die $_[0] };eval $proggie;</pre></blockquote>This is similar to saying:<blockquote><pre class="programlisting">use warnings qw/FATAL all/;eval $proggie;</pre></blockquote>except that the first has dynamic scope, whereas the second has lexical scope.</p><p><a name="INDEX-4583"></a>The routine indicated by <tt class="literal">$SIG{__DIE__}</tt> provides a way to turn a frogexception into a prince exception with a magical kiss, which oftendoesn't work.  The best use is for a moribund program that's about todie of an untrapped exception to do some last-moment processing on itsway out.  You can't save yourself this way, but you can give one lasthurrah.</p><p>The exception message is passed as the first argument.  When a<tt class="literal">__DIE__</tt> hook routine returns, exception processing continues asit would have in the absence of the hook, unless the hook routineitself exits via a <tt class="literal">goto</tt>, a loop exit, or a <tt class="literal">die</tt>.  The <tt class="literal">__DIE__</tt>handler is explicitly disabled during the call, so that you yourselfcan then call the real <tt class="literal">die</tt> from a <tt class="literal">__DIE__</tt> handler.  (If itweren't disabled, the handler would call itself recursively forever.) The handler for <tt class="literal">$SIG{__WARN__}</tt> works similarly.</p><p>Only the main program should set <tt class="literal">$SIG{__DIE__}</tt>, not modules. That's because currently, even exceptions that are being trappedstill trigger a <tt class="literal">$SIG{__DIE__}</tt> handler.  This is strongly discouragedbecause of its potential for breaking innocent modules who aren'texpecting their predicted exceptions to be mysteriously altered. Use this feature only as a last resort, and if you must, always puta <tt class="literal">local</tt> on the front to limit the period of danger.</p><p>Do not attempt to build an exception-handling mechanism on this feature.Use <tt class="literal">eval {}</tt> to trap exceptions instead.</p></dd><dt><b><tt class="literal">STDERR</tt></b></dt><dd><p><a name="INDEX-4584"></a>[ALL] The special filehandle for standard error in any package.</p></dd><dt><b><tt class="literal">STDIN</tt></b></dt><dd><p><a name="INDEX-4585"></a>[ALL] The special filehandle for standard input in any package.</p></dd><dt><b><tt class="literal">STDOUT</tt></b></dt><dd><p><a name="INDEX-4586"></a>[ALL] The special filehandle for standard output in any package.</

⌨️ 快捷键说明

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