perlfaq8.html
来自「perl教程」· HTML 代码 · 共 800 行 · 第 1/5 页
HTML
800 行
<span class="comment"># DO YOUR OPERATION HERE #</span>
<span class="comment">##########################</span>
</pre>
<pre>
<span class="keyword">syscall</span><span class="operator">(</span> <span class="operator">&</span><span class="variable">SYS_gettimeofday</span><span class="operator">,</span> <span class="variable">$done</span><span class="operator">,</span> <span class="number">0</span><span class="operator">)</span> <span class="operator">!=</span> <span class="operator">-</span><span class="number">1</span>
<span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"gettimeofday: $!"</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">@start</span> <span class="operator">=</span> <span class="keyword">unpack</span><span class="operator">(</span><span class="variable">$TIMEVAL_T</span><span class="operator">,</span> <span class="variable">$start</span><span class="operator">);</span>
<span class="variable">@done</span> <span class="operator">=</span> <span class="keyword">unpack</span><span class="operator">(</span><span class="variable">$TIMEVAL_T</span><span class="operator">,</span> <span class="variable">$done</span><span class="operator">);</span>
</pre>
<pre>
<span class="comment"># fix microseconds</span>
<span class="keyword">for</span> <span class="operator">(</span><span class="variable">$done</span><span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">,</span> <span class="variable">$start</span><span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">)</span> <span class="operator">{</span> <span class="variable">$_</span> <span class="operator">/=</span> <span class="number">1_000_000</span> <span class="operator">}</span>
</pre>
<pre>
<span class="variable">$delta_time</span> <span class="operator">=</span> <span class="keyword">sprintf</span> <span class="string">"%.4f"</span><span class="operator">,</span> <span class="operator">(</span><span class="variable">$done</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span> <span class="operator">+</span> <span class="variable">$done</span><span class="operator">[</span><span class="number">1</span><span class="operator">]</span> <span class="operator">)</span>
<span class="operator">-</span>
<span class="operator">(</span><span class="variable">$start</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span> <span class="operator">+</span> <span class="variable">$start</span><span class="operator">[</span><span class="number">1</span><span class="operator">]</span> <span class="operator">);</span>
</pre>
<p>
</p>
<h2><a name="how_can_i_do_an_atexit___or_setjmp___longjmp____exception_handling_">How can I do an <code>atexit()</code> or setjmp()/longjmp()? (Exception handling)</a></h2>
<p>Release 5 of Perl added the END block, which can be used to simulate
atexit(). Each package's END block is called when the program or
thread ends (see <a href="../../lib/Pod/perlmod.html">the perlmod manpage</a> manpage for more details).</p>
<p>For example, you can use this to make sure your filter program
managed to finish its output without filling up the disk:</p>
<pre>
<span class="keyword">END</span> <span class="operator">{</span>
<span class="keyword">close</span><span class="operator">(</span><span class="variable">STDOUT</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"stdout close failed: $!"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>The END block isn't called when untrapped signals kill the program,
though, so if you use END blocks you should also use</p>
<pre>
<span class="keyword">use</span> <span class="variable">sigtrap</span> <span class="string">qw(die normal-signals)</span><span class="operator">;</span>
</pre>
<p>Perl's exception-handling mechanism is its <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a> operator. You can
use <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a> as setjmp and <a href="../../lib/Pod/perlfunc.html#item_die"><code>die()</code></a> as longjmp. For details of this, see
the section on signals, especially the time-out handler for a blocking
<a href="../../lib/Pod/perlfunc.html#item_flock"><code>flock()</code></a> in <a href="../../lib/Pod/perlipc.html#signals">Signals in the perlipc manpage</a> or the section on "Signals" in
the Camel Book.</p>
<p>If exception handling is all you're interested in, try the
exceptions.pl library (part of the standard perl distribution).</p>
<p>If you want the <code>atexit()</code> syntax (and an <code>rmexit()</code> as well), try the
AtExit module available from CPAN.</p>
<p>
</p>
<h2><a name="why_doesn_t_my_sockets_program_work_under_system_v__solaris__what_does_the_error_message_protocol_not_supported_mean">Why doesn't my sockets program work under System V (Solaris)? What does the error message "Protocol not supported" mean?</a></h2>
<p>Some Sys-V based systems, notably Solaris 2.X, redefined some of the
standard socket constants. Since these were constant across all
architectures, they were often hardwired into perl code. The proper
way to deal with this is to "use Socket" to get the correct values.</p>
<p>Note that even though SunOS and Solaris are binary compatible, these
values are different. Go figure.</p>
<p>
</p>
<h2><a name="how_can_i_call_my_system_s_unique_c_functions_from_perl">How can I call my system's unique C functions from Perl?</a></h2>
<p>In most cases, you write an external module to do it--see the answer
to "Where can I learn about linking C with Perl? [h2xs, xsubpp]".
However, if the function is a system call, and your system supports
syscall(), you can use the syscall function (documented in
<a href="../../lib/Pod/perlfunc.html">the perlfunc manpage</a>).</p>
<p>Remember to check the modules that came with your distribution, and
CPAN as well---someone may already have written a module to do it. On
Windows, try Win32::API. On Macs, try Mac::Carbon. If no module
has an interface to the C function, you can inline a bit of C in your
Perl source with Inline::C.</p>
<p>
</p>
<h2><a name="where_do_i_get_the_include_files_to_do_ioctl___or_syscall__">Where do I get the include files to do <a href="../../lib/Pod/perlfunc.html#item_ioctl"><code>ioctl()</code></a> or syscall()?</a></h2>
<p>Historically, these would be generated by the h2ph tool, part of the
standard perl distribution. This program converts <code>cpp(1)</code> directives
in C header files to files containing subroutine definitions, like
&SYS_getitimer, which you can use as arguments to your functions.
It doesn't work perfectly, but it usually gets most of the job done.
Simple files like <em>errno.h</em>, <em>syscall.h</em>, and <em>socket.h</em> were fine,
but the hard ones like <em>ioctl.h</em> nearly always need to hand-edited.
Here's how to install the *.ph files:</p>
<pre>
1. become super-user
2. cd /usr/include
3. h2ph *.h */*.h</pre>
<p>If your system supports dynamic loading, for reasons of portability and
sanity you probably ought to use h2xs (also part of the standard perl
distribution). This tool converts C header files to Perl extensions.
See <a href="../../lib/Pod/perlxstut.html">the perlxstut manpage</a> for how to get started with h2xs.</p>
<p>If your system doesn't support dynamic loading, you still probably
ought to use h2xs. See <a href="../../lib/Pod/perlxstut.html">the perlxstut manpage</a> and <a href="../../lib/ExtUtils/MakeMaker.html">the ExtUtils::MakeMaker manpage</a> for
more information (in brief, just use <strong>make perl</strong> instead of a plain
<strong>make</strong> to rebuild perl with a new static extension).</p>
<p>
</p>
<h2><a name="why_do_setuid_perl_scripts_complain_about_kernel_problems">Why do setuid perl scripts complain about kernel problems?</a></h2>
<p>Some operating systems have bugs in the kernel that make setuid
scripts inherently insecure. Perl gives you a number of options
(described in <a href="../../lib/Pod/perlsec.html">the perlsec manpage</a>) to work around such systems.</p>
<p>
</p>
<h2><a name="how_can_i_open_a_pipe_both_to_and_from_a_command">How can I open a pipe both to and from a command?</a></h2>
<p>The IPC::Open2 module (part of the standard perl distribution) is an
easy-to-use approach that internally uses pipe(), fork(), and <a href="../../lib/Pod/perlfunc.html#item_exec"><code>exec()</code></a> to do
the job. Make sure you read the deadlock warnings in its documentation,
though (see <a href="../../lib/IPC/Open2.html">the IPC::Open2 manpage</a>). See
<a href="../../lib/Pod/perlipc.html#bidirectional_communication_with_another_process">Bidirectional Communication with Another Process in the perlipc manpage</a> and
<a href="../../lib/Pod/perlipc.html#bidirectional_communication_with_yourself">Bidirectional Communication with Yourself in the perlipc manpage</a></p>
<p>You may also use the IPC::Open3 module (part of the standard perl
distribution), but be warned that it has a different order of
arguments from IPC::Open2 (see <a href="../../lib/IPC/Open3.html">the IPC::Open3 manpage</a>).</p>
<p>
</p>
<h2><a name="why_can_t_i_get_the_output_of_a_command_with_system__">Why can't I get the output of a command with system()?</a></h2>
<p>You're confusing the purpose of <a href="../../lib/Pod/perlfunc.html#item_system"><code>system()</code></a> and backticks (``). <a href="../../lib/Pod/perlfunc.html#item_system"><code>system()</code></a>
runs a command and returns exit status information (as a 16 bit value:
the low 7 bits are the signal the process died from, if any, and
the high 8 bits are the actual exit value). Backticks (``) run a
command and return what it sent to STDOUT.</p>
<pre>
<span class="variable">$exit_status</span> <span class="operator">=</span> <span class="keyword">system</span><span class="operator">(</span><span class="string">"mail-users"</span><span class="operator">);</span>
<span class="variable">$output_string</span> <span class="operator">=</span> <span class="string">`ls`</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="how_can_i_capture_stderr_from_an_external_command">How can I capture STDERR from an external command?</a></h2>
<p>There are three basic ways of running external commands:</p>
<pre>
<span class="keyword">system</span> <span class="variable">$cmd</span><span class="operator">;</span> <span class="comment"># using system()</span>
<span class="variable">$output</span> <span class="operator">=</span> <span class="string">`$cmd`</span><span class="operator">;</span> <span class="comment"># using backticks (``)</span>
<span class="keyword">open</span> <span class="operator">(</span><span class="variable">PIPE</span><span class="operator">,</span> <span class="string">"cmd |"</span><span class="operator">);</span> <span class="comment"># using open()</span>
</pre>
<p>With system(), both STDOUT and STDERR will go the same place as the
script's STDOUT and STDERR, unless the <a href="../../lib/Pod/perlfunc.html#item_system"><code>system()</code></a> command redirects them.
Backticks and <a href="../../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> read <strong>only</strong> the STDOUT of your command.</p>
<p>You can also use the <code>open3()</code> function from IPC::Open3. Benjamin
Goldberg provides some sample code:</p>
<p>To capture a program's STDOUT, but discard its STDERR:</p>
<pre>
<span class="keyword">use</span> <span class="variable">IPC::Open3</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">File::Spec</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Symbol</span> <span class="string">qw(gensym)</span><span class="operator">;</span>
<span class="keyword">open</span><span class="operator">(</span><span class="keyword">NULL</span><span class="operator">,</span> <span class="string">">"</span><span class="operator">,</span> <span class="variable">File::Spec</span><span class="operator">-></span><span class="variable">devnull</span><span class="operator">);</span>
<span class="keyword">my</span> <span class="variable">$pid</span> <span class="operator">=</span> <span class="variable">open3</span><span class="operator">(</span><span class="variable">gensym</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">*PH</span><span class="operator">,</span> <span class="string">">&NULL"</span><span class="operator">,</span> <span class="string">"cmd"</span><span class="operator">);</span>
<span class="keyword">while</span><span class="operator">(</span> <span class="operator"><</span><span class="variable">PH</span><span class="operator">></span> <span class="operator">)</span> <span class="operator">{</span> <span class="operator">}</span>
<span class="keyword">waitpid</span><span class="operator">(</span><span class="variable">$pid</span><span class="operator">,</span> <span class="number">0</span><span class="operator">);</span>
</pre>
<p>To capture a program's STDERR, but discard its STDOUT:</p>
<pre>
<span class="keyword">use</span> <span class="variable">IPC::Open3</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">File::Spec</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Symbol</span> <span class="string">qw(gensym)</span><span class="operator">;</span>
<span class="keyword">open</span><span class="operator">(</span><span class="keyword">NULL</span><span class="operator">,</span> <span class="string">">"</span><span class="operator">,</span> <span class="variable">File::Spec</span><span class="operator">-></span><span class="variable">devnull</span><span class="operator">);</span>
<span class="keyword">my</span> <span class="variable">$pid</span> <span class="operator">=</span> <span class="variable">open3</span><span class="operator">(</span><span class="variable">gensym</span><span class="operator">,</span> <span class="string">">&NULL"</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">*PH</span><span class="operator">,</span> <span class="string">"cmd"</span><span class="operator">);</span>
<span class="keyword">while</span><span class="operator">(</span> <span class="operator"><</span><span class="variable">PH</span><span class="operator">></span> <span class="operator">)</span> <span class="operator">{</span> <span class="operator">}</span>
<span class="keyword">waitpid</span><span class="operator">(</span><span class="variable">$pid</span><span class="operator">,</span> <span class="number">0</span><span class="operator">);</span>
</pre>
<p>To capture a program's STDERR, and let its STDOUT go to our own STDERR:</p>
<pre>
<span class="keyword">use</span> <span class="variable">IPC::Open3</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="v
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?