perlfunc.html
来自「perl教程」· HTML 代码 · 共 891 行 · 第 1/5 页
HTML
891 行
<dd>
<pre>
-f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO), or Filehandle is a pipe.
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.</pre>
</dd>
<dd>
<pre>
-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.</pre>
</dd>
<dd>
<pre>
-T File is an ASCII text file (heuristic guess).
-B File is a "binary" file (opposite of -T).</pre>
</dd>
<dd>
<pre>
-M Script start time minus file modification time, in days.
-A Same for access time.
-C Same for inode change time (Unix, may differ for other platforms)</pre>
</dd>
<dd>
<p>Example:</p>
</dd>
<dd>
<pre>
<span class="keyword">while</span> <span class="operator">(<>)</span> <span class="operator">{</span>
<span class="keyword">chomp</span><span class="operator">;</span>
<span class="keyword">next</span> <span class="keyword">unless</span> <span class="keyword">-f</span> <span class="variable">$_</span><span class="operator">;</span> <span class="comment"># ignore specials</span>
<span class="comment">#...</span>
<span class="operator">}</span>
</pre>
</dd>
<dd>
<p>The interpretation of the file permission operators <code>-r</code>, <code>-R</code>,
<a href="../../lib/Pod/perlrun.html#item__2dw"><code>-w</code></a>, <a href="../../lib/Pod/perlrun.html#item__2dw"><code>-W</code></a>, <a href="../../lib/Pod/perlrun.html#item__2dx"><code>-x</code></a>, and <a href="#item__2dx"><code>-X</code></a> is by default based solely on the mode
of the file and the uids and gids of the user. There may be other
reasons you can't actually read, write, or execute the file. Such
reasons may be for example network filesystem access controls, ACLs
(access control lists), read-only filesystems, and unrecognized
executable formats.</p>
</dd>
<dd>
<p>Also note that, for the superuser on the local filesystems, the <code>-r</code>,
<code>-R</code>, <a href="../../lib/Pod/perlrun.html#item__2dw"><code>-w</code></a>, and <a href="../../lib/Pod/perlrun.html#item__2dw"><code>-W</code></a> tests always return 1, and <a href="../../lib/Pod/perlrun.html#item__2dx"><code>-x</code></a> and <a href="#item__2dx"><code>-X</code></a> return 1
if any execute bit is set in the mode. Scripts run by the superuser
may thus need to do a <a href="#item_stat"><code>stat()</code></a> to determine the actual mode of the file,
or temporarily set their effective uid to something else.</p>
</dd>
<dd>
<p>If you are using ACLs, there is a pragma called <code>filetest</code> that may
produce more accurate results than the bare <a href="#item_stat"><code>stat()</code></a> mode bits.
When under the <code>use filetest 'access'</code> the above-mentioned filetests
will test whether the permission can (not) be granted using the
<code>access()</code> family of system calls. Also note that the <a href="../../lib/Pod/perlrun.html#item__2dx"><code>-x</code></a> and <a href="#item__2dx"><code>-X</code></a> may
under this pragma return true even if there are no execute permission
bits set (nor any extra execute permission ACLs). This strangeness is
due to the underlying system calls' definitions. Read the
documentation for the <code>filetest</code> pragma for more information.</p>
</dd>
<dd>
<p>Note that <code>-s/a/b/</code> does not do a negated substitution. Saying
<a href="#item_exp"><code>-exp($foo)</code></a> still works as expected, however--only single letters
following a minus are interpreted as file tests.</p>
</dd>
<dd>
<p>The <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-T</code></a> and <code>-B</code> switches work as follows. The first block or so of the
file is examined for odd characters such as strange control codes or
characters with the high bit set. If too many strange characters (>30%)
are found, it's a <code>-B</code> file; otherwise it's a <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-T</code></a> file. Also, any file
containing null in the first block is considered a binary file. If <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-T</code></a>
or <code>-B</code> is used on a filehandle, the current IO buffer is examined
rather than the first block. Both <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-T</code></a> and <code>-B</code> return true on a null
file, or a file at EOF when testing a filehandle. Because you have to
read a file to do the <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-T</code></a> test, on most occasions you want to use a <a href="../../lib/Pod/perlrun.html#item__2df"><code>-f</code></a>
against the file first, as in <code>next unless -f $file && -T $file</code>.</p>
</dd>
<dd>
<p>If any of the file tests (or either the <a href="#item_stat"><code>stat</code></a> or <a href="#item_lstat"><code>lstat</code></a> operators) are given
the special filehandle consisting of a solitary underline, then the stat
structure of the previous file test (or stat operator) is used, saving
a system call. (This doesn't work with <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-t</code></a>, and you need to remember
that <a href="#item_lstat"><code>lstat()</code></a> and <code>-l</code> will leave values in the stat structure for the
symbolic link, not the real file.) (Also, if the stat buffer was filled by
an <a href="#item_lstat"><code>lstat</code></a> call, <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-T</code></a> and <code>-B</code> will reset it with the results of <code>stat _</code>).
Example:</p>
</dd>
<dd>
<pre>
<span class="keyword">print</span> <span class="string">"Can do.\n"</span> <span class="keyword">if</span> <span class="keyword">-r</span> <span class="variable">$a</span> <span class="operator">||</span> <span class="keyword">-w</span> <span class="variable">_</span> <span class="operator">||</span> <span class="keyword">-x</span> <span class="variable">_</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<pre>
<span class="keyword">stat</span><span class="operator">(</span><span class="variable">$filename</span><span class="operator">);</span>
<span class="keyword">print</span> <span class="string">"Readable\n"</span> <span class="keyword">if</span> <span class="keyword">-r</span> <span class="variable">_</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"Writable\n"</span> <span class="keyword">if</span> <span class="keyword">-w</span> <span class="variable">_</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"Executable\n"</span> <span class="keyword">if</span> <span class="keyword">-x</span> <span class="variable">_</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"Setuid\n"</span> <span class="keyword">if</span> <span class="keyword">-u</span> <span class="variable">_</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"Setgid\n"</span> <span class="keyword">if</span> <span class="keyword">-g</span> <span class="variable">_</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"Sticky\n"</span> <span class="keyword">if</span> <span class="keyword">-k</span> <span class="variable">_</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"Text\n"</span> <span class="keyword">if</span> <span class="keyword">-T</span> <span class="variable">_</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"Binary\n"</span> <span class="keyword">if</span> <span class="keyword">-B</span> <span class="variable">_</span><span class="operator">;</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_abs">abs VALUE</a></strong>
<dt><strong>abs</strong>
<dd>
<p>Returns the absolute value of its argument.
If VALUE is omitted, uses <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_accept">accept NEWSOCKET,GENERICSOCKET</a></strong>
<dd>
<p>Accepts an incoming socket connect, just as the <a href="#item_accept"><code>accept(2)</code></a> system call
does. Returns the packed address if it succeeded, false otherwise.
See the example in <a href="../../lib/Pod/perlipc.html#sockets__client_server_communication">Sockets: Client/Server Communication in the perlipc manpage</a>.</p>
</dd>
<dd>
<p>On systems that support a close-on-exec flag on files, the flag will
be set for the newly opened file descriptor, as determined by the
value of $^F. See <a href="../../lib/Pod/perlvar.html#__f">$^F in the perlvar manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_alarm">alarm SECONDS</a></strong>
<dt><strong>alarm</strong>
<dd>
<p>Arranges to have a SIGALRM delivered to this process after the
specified number of wallclock seconds has elapsed. If SECONDS is not
specified, the value stored in <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a> is used. (On some machines,
unfortunately, the elapsed time may be up to one second less or more
than you specified because of how seconds are counted, and process
scheduling may delay the delivery of the signal even further.)</p>
</dd>
<dd>
<p>Only one timer may be counting at once. Each call disables the
previous timer, and an argument of <code>0</code> may be supplied to cancel the
previous timer without starting a new one. The returned value is the
amount of time remaining on the previous timer.</p>
</dd>
<dd>
<p>For delays of finer granularity than one second, you may use Perl's
four-argument version of <a href="#item_select"><code>select()</code></a> leaving the first three arguments
undefined, or you might be able to use the <a href="#item_syscall"><code>syscall</code></a> interface to
access <code>setitimer(2)</code> if your system supports it. The Time::HiRes
module (from CPAN, and starting from Perl 5.8 part of the standard
distribution) may also prove useful.</p>
</dd>
<dd>
<p>It is usually a mistake to intermix <a href="#item_alarm"><code>alarm</code></a> and <a href="#item_sleep"><code>sleep</code></a> calls.
(<a href="#item_sleep"><code>sleep</code></a> may be internally implemented in your system with <a href="#item_alarm"><code>alarm</code></a>)</p>
</dd>
<dd>
<p>If you want to use <a href="#item_alarm"><code>alarm</code></a> to time out a system call you need to use an
<a href="#item_eval"><code>eval</code></a>/<a href="#item_die"><code>die</code></a> pair. You can't rely on the alarm causing the system call to
fail with <a href="../../lib/Pod/perlvar.html#item___"><code>$!</code></a> set to <code>EINTR</code> because Perl sets up signal handlers to
restart system calls on some systems. Using <a href="#item_eval"><code>eval</code></a>/<a href="#item_die"><code>die</code></a> always works,
modulo the caveats given in <a href="../../lib/Pod/perlipc.html#signals">Signals in the perlipc manpage</a>.</p>
</dd>
<dd>
<pre>
<span class="keyword">eval</span> <span class="operator">{</span>
<span class="keyword">local</span> <span class="variable">$SIG</span><span class="operator">{</span><span class="string">ALRM</span><span class="operator">}</span> <span class="operator">=</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="keyword">die</span> <span class="string">"alarm\n"</span> <span class="operator">};</span> <span class="comment"># NB: \n required</span>
<span class="keyword">alarm</span> <span class="variable">$timeout</span><span class="operator">;</span>
<span class="variable">$nread</span> <span class="operator">=</span> <span class="keyword">sysread</span> <span class="variable">SOCKET</span><span class="operator">,</span> <span class="variable">$buffer</span><span class="operator">,</span> <span class="variable">$size</span><span class="operator">;</span>
<span class="keyword">alarm</span> <span class="number">0</span><span class="operator">;</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?