perlvar.html
来自「perl教程」· HTML 代码 · 共 1,313 行 · 第 1/5 页
HTML
1,313 行
</dd>
<dd>
<p>Also see <a href="#error_indicators">Error Indicators</a>.</p>
</dd>
</li>
<dt><strong><a name="item____encoding_">${^ENCODING}</a></strong>
<dd>
<p>The <em>object reference</em> to the Encode object that is used to convert
the source code to Unicode. Thanks to this variable your perl script
does not have to be written in UTF-8. Default is <em>undef</em>. The direct
manipulation of this variable is highly discouraged. See <a href="../../lib/encoding.html">the encoding manpage</a>
for more details.</p>
</dd>
</li>
<dt><strong><a name="item__os_error">$OS_ERROR</a></strong>
<dt><strong><a name="item__errno">$ERRNO</a></strong>
<dt><strong><a name="item___">$!</a></strong>
<dd>
<p>If used numerically, yields the current value of the C <code>errno</code>
variable, or in other words, if a system or library call fails, it
sets this variable. This means that the value of <a href="#item___"><code>$!</code></a> is meaningful
only <em>immediately</em> after a <strong>failure</strong>:</p>
</dd>
<dd>
<pre>
<span class="keyword">if</span> <span class="operator">(</span><span class="keyword">open</span><span class="operator">(</span><span class="variable">FH</span><span class="operator">,</span> <span class="variable">$filename</span><span class="operator">))</span> <span class="operator">{</span>
<span class="comment"># Here $! is meaningless.</span>
<span class="operator">...</span>
<span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
<span class="comment"># ONLY here is $! meaningful.</span>
<span class="operator">...</span>
<span class="comment"># Already here $! might be meaningless.</span>
<span class="operator">}</span>
<span class="comment"># Since here we might have either success or failure,</span>
<span class="comment"># here $! is meaningless.</span>
</pre>
</dd>
<dd>
<p>In the above <em>meaningless</em> stands for anything: zero, non-zero,
<a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>. A successful system or library call does <strong>not</strong> set
the variable to zero.</p>
</dd>
<dd>
<p>If used as a string, yields the corresponding system error string.
You can assign a number to <a href="#item___"><code>$!</code></a> to set <em>errno</em> if, for instance,
you want <code>"$!"</code> to return the string for error <em>n</em>, or you want
to set the exit value for the <a href="../../lib/Pod/perlfunc.html#item_die"><code>die()</code></a> operator. (Mnemonic: What just
went bang?)</p>
</dd>
<dd>
<p>Also see <a href="#error_indicators">Error Indicators</a>.</p>
</dd>
</li>
<dt><strong><a name="item___">%!</a></strong>
<dd>
<p>Each element of <a href="#item___"><code>%!</code></a> has a true value only if <a href="#item___"><code>$!</code></a> is set to that
value. For example, <code>$!{ENOENT}</code> is true if and only if the current
value of <a href="#item___"><code>$!</code></a> is <code>ENOENT</code>; that is, if the most recent error was
"No such file or directory" (or its moral equivalent: not all operating
systems give that exact error, and certainly not all languages).
To check if a particular key is meaningful on your system, use
<code>exists $!{the_key}</code>; for a list of legal keys, use <code>keys %!</code>.
See <a href="../../lib/Errno.html">the Errno manpage</a> for more information, and also see above for the
validity of <a href="#item___"><code>$!</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item__extended_os_error">$EXTENDED_OS_ERROR</a></strong>
<dt><strong><a name="item___e">$^E</a></strong>
<dd>
<p>Error information specific to the current operating system. At
the moment, this differs from <a href="#item___"><code>$!</code></a> under only VMS, OS/2, and Win32
(and for MacPerl). On all other platforms, <a href="#item___e"><code>$^E</code></a> is always just
the same as <a href="#item___"><code>$!</code></a>.</p>
</dd>
<dd>
<p>Under VMS, <a href="#item___e"><code>$^E</code></a> provides the VMS status value from the last
system error. This is more specific information about the last
system error than that provided by <a href="#item___"><code>$!</code></a>. This is particularly
important when <a href="#item___"><code>$!</code></a> is set to <strong>EVMSERR</strong>.</p>
</dd>
<dd>
<p>Under OS/2, <a href="#item___e"><code>$^E</code></a> is set to the error code of the last call to
OS/2 API either via CRT, or directly from perl.</p>
</dd>
<dd>
<p>Under Win32, <a href="#item___e"><code>$^E</code></a> always returns the last error information
reported by the Win32 call <code>GetLastError()</code> which describes
the last error from within the Win32 API. Most Win32-specific
code will report errors via <a href="#item___e"><code>$^E</code></a>. ANSI C and Unix-like calls
set <code>errno</code> and so most portable Perl code will report errors
via <a href="#item___"><code>$!</code></a>.</p>
</dd>
<dd>
<p>Caveats mentioned in the description of <a href="#item___"><code>$!</code></a> generally apply to
<a href="#item___e"><code>$^E</code></a>, also. (Mnemonic: Extra error explanation.)</p>
</dd>
<dd>
<p>Also see <a href="#error_indicators">Error Indicators</a>.</p>
</dd>
</li>
<dt><strong><a name="item__eval_error">$EVAL_ERROR</a></strong>
<dt><strong><a name="item___">$@</a></strong>
<dd>
<p>The Perl syntax error message from the last <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a> operator.
If $@ is the null string, the last <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a> parsed and executed
correctly (although the operations you invoked may have failed in the
normal fashion). (Mnemonic: Where was the syntax error "at"?)</p>
</dd>
<dd>
<p>Warning messages are not collected in this variable. You can,
however, set up a routine to process warnings by setting <code>$SIG{__WARN__}</code>
as described below.</p>
</dd>
<dd>
<p>Also see <a href="#error_indicators">Error Indicators</a>.</p>
</dd>
</li>
<dt><strong><a name="item__process_id">$PROCESS_ID</a></strong>
<dt><strong><a name="item__pid">$PID</a></strong>
<dt><strong><a name="item___">$$</a></strong>
<dd>
<p>The process number of the Perl running this script. You should
consider this variable read-only, although it will be altered
across <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> calls. (Mnemonic: same as shells.)</p>
</dd>
<dd>
<p>Note for Linux users: on Linux, the C functions <code>getpid()</code> and
<a href="../../lib/Pod/perlfunc.html#item_getppid"><code>getppid()</code></a> return different values from different threads. In order to
be portable, this behavior is not reflected by <a href="#item___"><code>$$</code></a>, whose value remains
consistent across threads. If you want to call the underlying <code>getpid()</code>,
you may use the CPAN module <code>Linux::Pid</code>.</p>
</dd>
</li>
<dt><strong><a name="item__real_user_id">$REAL_USER_ID</a></strong>
<dt><strong><a name="item__uid">$UID</a></strong>
<dt><strong><a name="item___">$<</a></strong>
<dd>
<p>The real uid of this process. (Mnemonic: it's the uid you came <em>from</em>,
if you're running setuid.) You can change both the real uid and
the effective uid at the same time by using POSIX::setuid(). Since
changes to $< require a system call, check $! after a change attempt to
detect any possible errors.</p>
</dd>
</li>
<dt><strong><a name="item__effective_user_id">$EFFECTIVE_USER_ID</a></strong>
<dt><strong><a name="item__euid">$EUID</a></strong>
<dt><strong><a name="item___">$></a></strong>
<dd>
<p>The effective uid of this process. Example:</p>
</dd>
<dd>
<pre>
<span class="variable">$<</span> <span class="operator">=</span> <span class="variable">$></span><span class="operator">;</span> <span class="comment"># set real to effective uid</span>
<span class="operator">(</span><span class="variable">$<</span><span class="operator">,</span><span class="variable">$></span><span class="operator">)</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$></span><span class="operator">,</span><span class="variable">$<</span><span class="operator">);</span> <span class="comment"># swap real and effective uid</span>
</pre>
</dd>
<dd>
<p>You can change both the effective uid and the real uid at the same
time by using POSIX::setuid(). Changes to $> require a check to $!
to detect any possible errors after an attempted change.</p>
</dd>
<dd>
<p>(Mnemonic: it's the uid you went <em>to</em>, if you're running setuid.)
<a href="#item___"><code>$<</code></a> and <a href="#item___"><code>$></code></a> can be swapped only on machines
supporting setreuid().</p>
</dd>
</li>
<dt><strong><a name="item__real_group_id">$REAL_GROUP_ID</a></strong>
<dt><strong><a name="item__gid">$GID</a></strong>
<dt><strong><a name="item___">$(</a></strong>
<dd>
<p>The real gid of this process. If you are on a machine that supports
membership in multiple groups simultaneously, gives a space separated
list of groups you are in. The first number is the one returned by
getgid(), and the subsequent ones by getgroups(), one of which may be
the same as the first number.</p>
</dd>
<dd>
<p>However, a value assigned to <a href="#item___"><code>$(</code></a> must be a single number used to
set the real gid. So the value given by <a href="#item___"><code>$(</code></a> should <em>not</em> be assigned
back to <a href="#item___"><code>$(</code></a> without being forced numeric, such as by adding zero.</p>
</dd>
<dd>
<p>You can change both the real gid and the effective gid at the same
time by using POSIX::setgid(). Changes to $( require a check to $!
to detect any possible errors after an attempted change.</p>
</dd>
<dd>
<p>(Mnemonic: parentheses are used to <em>group</em> things. The real gid is the
group you <em>left</em>, if you're running setgid.)</p>
</dd>
</li>
<dt><strong><a name="item__effective_group_id">$EFFECTIVE_GROUP_ID</a></strong>
<dt><strong><a name="item__egid">$EGID</a></strong>
<dt><strong><a name="item___">$)</a></strong>
<dd>
<p>The effective gid of this process. If you are on a machine that
supports membership in multiple groups simultaneously, gives a space
separated list of groups you are in. The first number is the one
returned by getegid(), and the subsequent ones by getgroups(), one of
which may be the same as the first number.</p>
</dd>
<dd>
<p>Similarly, a value assigned to <a href="#item___"><code>$)</code></a> must also be a space-separated
list of numbers. The first number sets the effective gid, and
the rest (if any) are passed to setgroups(). To get the effect of an
empty list for setgroups(), just repeat the new effective gid; that is,
to force an effective gid of 5 and an effectively empty <code>setgroups()</code>
list, say <code> $) = "5 5" </code>.</p>
</dd>
<dd>
<p>You can change both the effective gid and the real gid at the same
time by using POSIX::setgid() (use only a single numeric argument).
Changes to $) require a check to $! to detect any possible errors
after an attempted change.</p>
</dd>
<dd>
<p>(Mnemonic: parentheses are used to <em>group</em> things. The effective gid
is the group that's <em>right</em> for you, if you're running setgid.)</p>
</dd>
<dd>
<p><a href="#item___"><code>$<</code></a>, <a href="#item___"><code>$></code></a>, <a href="#item___"><code>$(</code></a> and <a href="#item___"><code>$)</code></a> can be set only on
machines that support the corresponding <em>set[re][ug]id()</em> routine. <a href="#item___"><code>$(</code></a>
and <a href="#item___"><code>$)</code></a> can be swapped only on machines supporting setregid().</p>
</dd>
</li>
<dt><strong><a name="item__program_name">$PROGRAM_NAME</a></strong>
<dt><strong><a name="item__0">$0</a></strong>
<dd>
<p>Contains the name of the program being executed.</p>
</dd>
<dd>
<p>On some (read: not all) operating systems assigning to <a href="#item__0"><code>$0</code></a> modifies
the argument area that the <code>ps</code> program sees. On some platforms you
may have to use special <code>ps</code> options or a different <code>ps</code> to see the
changes. Modifying the $0 is more useful as a way of indicating the
current program state than it is for hiding the program you're
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?