perlfunc.html

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

HTML
891 行
字号
</dd>
<dd>
<p>With EXPR, it returns some extra information that the debugger uses to
print a stack trace.  The value of EXPR indicates how many call frames
to go back before the current one.</p>
</dd>
<dd>
<pre>
    <span class="operator">(</span><span class="variable">$package</span><span class="operator">,</span> <span class="variable">$filename</span><span class="operator">,</span> <span class="variable">$line</span><span class="operator">,</span> <span class="variable">$subroutine</span><span class="operator">,</span> <span class="variable">$hasargs</span><span class="operator">,</span>
    <span class="variable">$wantarray</span><span class="operator">,</span> <span class="variable">$evaltext</span><span class="operator">,</span> <span class="variable">$is_require</span><span class="operator">,</span> <span class="variable">$hints</span><span class="operator">,</span> <span class="variable">$bitmask</span><span class="operator">)</span> <span class="operator">=</span> <span class="keyword">caller</span><span class="operator">(</span><span class="variable">$i</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Here $subroutine may be <code>(eval)</code> if the frame is not a subroutine
call, but an <a href="#item_eval"><code>eval</code></a>.  In such a case additional elements $evaltext and
<code>$is_require</code> are set: <code>$is_require</code> is true if the frame is created by a
<a href="#item_require"><code>require</code></a> or <a href="#item_use"><code>use</code></a> statement, $evaltext contains the text of the
<a href="#item_eval"><code>eval EXPR</code></a> statement.  In particular, for an <a href="#item_eval"><code>eval BLOCK</code></a> statement,
$filename is <code>(eval)</code>, but $evaltext is undefined.  (Note also that
each <a href="#item_use"><code>use</code></a> statement creates a <a href="#item_require"><code>require</code></a> frame inside an <a href="#item_eval"><code>eval EXPR</code></a>
frame.)  $subroutine may also be <code>(unknown)</code> if this particular
subroutine happens to have been deleted from the symbol table.
<code>$hasargs</code> is true if a new instance of <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a> was set up for the frame.
<code>$hints</code> and <code>$bitmask</code> contain pragmatic hints that the caller was
compiled with.  The <code>$hints</code> and <code>$bitmask</code> values are subject to change
between versions of Perl, and are not meant for external use.</p>
</dd>
<dd>
<p>Furthermore, when called from within the DB package, caller returns more
detailed information: it sets the list variable <code>@DB::args</code> to be the
arguments with which the subroutine was invoked.</p>
</dd>
<dd>
<p>Be aware that the optimizer might have optimized call frames away before
<a href="#item_caller"><code>caller</code></a> had a chance to get the information.  That means that <a href="#item_caller"><code>caller(N)</code></a>
might not return information about the call frame you expect it do, for
<code>N &gt; 1</code>.  In particular, <code>@DB::args</code> might have information from the
previous time <a href="#item_caller"><code>caller</code></a> was called.</p>
</dd>
</li>
<dt><strong><a name="item_chdir">chdir EXPR</a></strong>

<dt><strong>chdir FILEHANDLE</strong>

<dt><strong>chdir DIRHANDLE</strong>

<dt><strong>chdir</strong>

<dd>
<p>Changes the working directory to EXPR, if possible. If EXPR is omitted,
changes to the directory specified by <code>$ENV{HOME}</code>, if set; if not,
changes to the directory specified by <code>$ENV{LOGDIR}</code>. (Under VMS, the
variable <code>$ENV{SYS$LOGIN}</code> is also checked, and used if it is set.) If
neither is set, <a href="#item_chdir"><code>chdir</code></a> does nothing. It returns true upon success,
false otherwise. See the example under <a href="#item_die"><code>die</code></a>.</p>
</dd>
<dd>
<p>On systems that support fchdir, you might pass a file handle or
directory handle as argument.  On systems that don't support fchdir,
passing handles produces a fatal error at run time.</p>
</dd>
</li>
<dt><strong><a name="item_chmod">chmod LIST</a></strong>

<dd>
<p>Changes the permissions of a list of files.  The first element of the
list must be the numerical mode, which should probably be an octal
number, and which definitely should <em>not</em> be a string of octal digits:
<code>0644</code> is okay, <code>'0644'</code> is not.  Returns the number of files
successfully changed.  See also <a href="#item_oct">oct</a>, if all you have is a string.</p>
</dd>
<dd>
<pre>
    <span class="variable">$cnt</span> <span class="operator">=</span> <span class="keyword">chmod</span> <span class="number">0755</span><span class="operator">,</span> <span class="string">'foo'</span><span class="operator">,</span> <span class="string">'bar'</span><span class="operator">;</span>
    <span class="keyword">chmod</span> <span class="number">0755</span><span class="operator">,</span> <span class="variable">@executables</span><span class="operator">;</span>
    <span class="variable">$mode</span> <span class="operator">=</span> <span class="string">'0644'</span><span class="operator">;</span> <span class="keyword">chmod</span> <span class="variable">$mode</span><span class="operator">,</span> <span class="string">'foo'</span><span class="operator">;</span>      <span class="comment"># !!! sets mode to</span>
                                             <span class="comment"># --w----r-T</span>
    <span class="variable">$mode</span> <span class="operator">=</span> <span class="string">'0644'</span><span class="operator">;</span> <span class="keyword">chmod</span> <span class="keyword">oct</span><span class="operator">(</span><span class="variable">$mode</span><span class="operator">),</span> <span class="string">'foo'</span><span class="operator">;</span> <span class="comment"># this is better</span>
    <span class="variable">$mode</span> <span class="operator">=</span> <span class="number">0644</span><span class="operator">;</span>   <span class="keyword">chmod</span> <span class="variable">$mode</span><span class="operator">,</span> <span class="string">'foo'</span><span class="operator">;</span>      <span class="comment"># this is best</span>
</pre>
</dd>
<dd>
<p>On systems that support fchmod, you might pass file handles among the
files.  On systems that don't support fchmod, passing file handles
produces a fatal error at run time.</p>
</dd>
<dd>
<pre>
    <span class="keyword">open</span><span class="operator">(</span><span class="keyword">my</span> <span class="variable">$fh</span><span class="operator">,</span> <span class="string">"&lt;"</span><span class="operator">,</span> <span class="string">"foo"</span><span class="operator">);</span>
    <span class="keyword">my</span> <span class="variable">$perm</span> <span class="operator">=</span> <span class="operator">(</span><span class="keyword">stat</span> <span class="variable">$fh</span><span class="operator">)</span><span class="operator">[</span><span class="number">2</span><span class="operator">]</span> <span class="operator">&amp;</span> <span class="number">07777</span><span class="operator">;</span>
    <span class="keyword">chmod</span><span class="operator">(</span><span class="variable">$perm</span> <span class="operator">|</span> <span class="number">0600</span><span class="operator">,</span> <span class="variable">$fh</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>You can also import the symbolic <code>S_I*</code> constants from the Fcntl
module:</p>
</dd>
<dd>
<pre>
    <span class="keyword">use</span> <span class="variable">Fcntl</span> <span class="string">':mode'</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<pre>
    <span class="keyword">chmod</span> <span class="variable">S_IRWXU</span><span class="operator">|</span><span class="variable">S_IRGRP</span><span class="operator">|</span><span class="variable">S_IXGRP</span><span class="operator">|</span><span class="variable">S_IROTH</span><span class="operator">|</span><span class="variable">S_IXOTH</span><span class="operator">,</span> <span class="variable">@executables</span><span class="operator">;</span>
    <span class="comment"># This is identical to the chmod 0755 of the above example.</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_chomp">chomp VARIABLE</a></strong>

<dt><strong>chomp( LIST )</strong>

<dt><strong>chomp</strong>

<dd>
<p>This safer version of <a href="#item_chop">chop</a> removes any trailing string
that corresponds to the current value of <a href="../../lib/Pod/perlvar.html#item___"><code>$/</code></a> (also known as
$INPUT_RECORD_SEPARATOR in the <code>English</code> module).  It returns the total
number of characters removed from all its arguments.  It's often used to
remove the newline from the end of an input record when you're worried
that the final record may be missing its newline.  When in paragraph
mode (<a href="../../lib/Pod/perlvar.html#item___"><code>$/ = &quot;&quot;</code></a>), it removes all trailing newlines from the string.
When in slurp mode (<a href="../../lib/Pod/perlvar.html#item___"><code>$/ = undef</code></a>) or fixed-length record mode (<a href="../../lib/Pod/perlvar.html#item___"><code>$/</code></a> is
a reference to an integer or the like, see <a href="../../lib/Pod/perlvar.html">the perlvar manpage</a>) <a href="#item_chomp"><code>chomp()</code></a> won't
remove anything.
If VARIABLE is omitted, it chomps <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a>.  Example:</p>
</dd>
<dd>
<pre>
    <span class="keyword">while</span> <span class="operator">(&lt;&gt;)</span> <span class="operator">{</span>
        <span class="keyword">chomp</span><span class="operator">;</span>  <span class="comment"># avoid \n on last field</span>
        <span class="variable">@array</span> <span class="operator">=</span> <span class="keyword">split</span><span class="operator">(</span><span class="regex">/:/</span><span class="operator">);</span>
        <span class="comment"># ...</span>
    <span class="operator">}</span>
</pre>
</dd>
<dd>
<p>If VARIABLE is a hash, it chomps the hash's values, but not its keys.</p>
</dd>
<dd>
<p>You can actually chomp anything that's an lvalue, including an assignment:</p>
</dd>
<dd>
<pre>
    <span class="keyword">chomp</span><span class="operator">(</span><span class="variable">$cwd</span> <span class="operator">=</span> <span class="string">`pwd`</span><span class="operator">);</span>
    <span class="keyword">chomp</span><span class="operator">(</span><span class="variable">$answer</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">STDIN</span><span class="operator">&gt;);</span>
</pre>
</dd>
<dd>
<p>If you chomp a list, each element is chomped, and the total number of
characters removed is returned.</p>
</dd>
<dd>
<p>If the <code>encoding</code> pragma is in scope then the lengths returned are
calculated from the length of <a href="../../lib/Pod/perlvar.html#item___"><code>$/</code></a> in Unicode characters, which is not
always the same as the length of <a href="../../lib/Pod/perlvar.html#item___"><code>$/</code></a> in the native encoding.</p>
</dd>
<dd>
<p>Note that parentheses are necessary when you're chomping anything
that is not a simple variable.  This is because <code>chomp $cwd = `pwd`;</code>
is interpreted as <code>(chomp $cwd) = `pwd`;</code>, rather than as
<a href="#item_chomp"><code>chomp( $cwd = `pwd` )</code></a> which you might expect.  Similarly,
<code>chomp $a, $b</code> is interpreted as <a href="#item_chomp"><code>chomp($a), $b</code></a> rather than
as <a href="#item_chomp"><code>chomp($a, $b)</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_chop">chop VARIABLE</a></strong>

<dt><strong>chop( LIST )</strong>

<dt><strong>chop</strong>

<dd>
<p>Chops off the last 

⌨️ 快捷键说明

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