perlfaq8.html
来自「perl教程」· HTML 代码 · 共 800 行 · 第 1/5 页
HTML
800 行
IPC::Run, and some of the POE modules. See CPAN for more
details.</p>
<p>You could also use</p>
<pre>
system("cmd &")</pre>
<p>or you could use fork as documented in <a href="../../lib/Pod/perlfunc.html#fork">fork in the perlfunc manpage</a>, with
further examples in <a href="../../lib/Pod/perlipc.html">the perlipc manpage</a>. Some things to be aware of, if you're
on a Unix-like system:</p>
<dl>
<dt><strong><a name="item_stdin_2c_stdout_2c_and_stderr_are_shared">STDIN, STDOUT, and STDERR are shared</a></strong>
<dd>
<p>Both the main process and the backgrounded one (the "child" process)
share the same STDIN, STDOUT and STDERR filehandles. If both try to
access them at once, strange things can happen. You may want to close
or reopen these for the child. You can get around this with
<a href="../../lib/Pod/perlfunc.html#item_open"><code>open</code></a>ing a pipe (see <a href="../../lib/Pod/perlfunc.html#open">open in the perlfunc manpage</a>) but on some systems this
means that the child process cannot outlive the parent.</p>
</dd>
</li>
<dt><strong><a name="item_signals">Signals</a></strong>
<dd>
<p>You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too.
SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is
sent when you write to a filehandle whose child process has closed (an
untrapped SIGPIPE can cause your program to silently die). This is
not an issue with <a href="../../lib/Pod/perlfunc.html#item_system"><code>system("cmd&")</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_zombies">Zombies</a></strong>
<dd>
<p>You have to be prepared to "reap" the child process when it finishes.</p>
</dd>
<dd>
<pre>
<span class="variable">$SIG</span><span class="operator">{</span><span class="string">CHLD</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">wait</span> <span class="operator">};</span>
</pre>
</dd>
<dd>
<pre>
<span class="variable">$SIG</span><span class="operator">{</span><span class="string">CHLD</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">'IGNORE'</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>You can also use a double fork. You immediately <a href="../../lib/Pod/perlfunc.html#item_wait"><code>wait()</code></a> for your
first child, and the init daemon will <a href="../../lib/Pod/perlfunc.html#item_wait"><code>wait()</code></a> for your grandchild once
it exits.</p>
</dd>
<dd>
<pre>
<span class="keyword">unless</span> <span class="operator">(</span><span class="variable">$pid</span> <span class="operator">=</span> <span class="keyword">fork</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">unless</span> <span class="operator">(</span><span class="keyword">fork</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">exec</span> <span class="string">"what you really wanna do"</span><span class="operator">;</span>
<span class="keyword">die</span> <span class="string">"exec failed!"</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="keyword">exit</span> <span class="number">0</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>
</dd>
<dd>
<p>See <a href="../../lib/Pod/perlipc.html#signals">Signals in the perlipc manpage</a> for other examples of code to do this.
Zombies are not an issue with <a href="../../lib/Pod/perlfunc.html#item_system"><code>system("prog &")</code></a>.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="how_do_i_trap_control_characters_signals">How do I trap control characters/signals?</a></h2>
<p>You don't actually "trap" a control character. Instead, that character
generates a signal which is sent to your terminal's currently
foregrounded process group, which you then trap in your process.
Signals are documented in <a href="../../lib/Pod/perlipc.html#signals">Signals in the perlipc manpage</a> and the
section on "Signals" in the Camel.</p>
<p>You can set the values of the %SIG hash to be the functions you want
to handle the signal. After perl catches the signal, it looks in %SIG
for a key with the same name as the signal, then calls the subroutine
value for that key.</p>
<pre>
<span class="comment"># as an anonymous subroutine</span>
</pre>
<pre>
<span class="variable">$SIG</span><span class="operator">{</span><span class="string">INT</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">syswrite</span><span class="operator">(</span><span class="variable">STDERR</span><span class="operator">,</span> <span class="string">"ouch\n"</span><span class="operator">,</span> <span class="number">5</span> <span class="operator">)</span> <span class="operator">};</span>
</pre>
<pre>
<span class="comment"># or a reference to a function</span>
</pre>
<pre>
<span class="variable">$SIG</span><span class="operator">{</span><span class="string">INT</span><span class="operator">}</span> <span class="operator">=</span> <span class="operator">\&</span><span class="variable">ouch</span><span class="operator">;</span>
</pre>
<pre>
<span class="comment"># or the name of the function as a string</span>
</pre>
<pre>
<span class="variable">$SIG</span><span class="operator">{</span><span class="string">INT</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">"ouch"</span><span class="operator">;</span>
</pre>
<p>Perl versions before 5.8 had in its C source code signal handlers which
would catch the signal and possibly run a Perl function that you had set
in %SIG. This violated the rules of signal handling at that level
causing perl to dump core. Since version 5.8.0, perl looks at %SIG
*after* the signal has been caught, rather than while it is being caught.
Previous versions of this answer were incorrect.</p>
<p>
</p>
<h2><a name="how_do_i_modify_the_shadow_password_file_on_a_unix_system">How do I modify the shadow password file on a Unix system?</a></h2>
<p>If perl was installed correctly and your shadow library was written
properly, the getpw*() functions described in <a href="../../lib/Pod/perlfunc.html">the perlfunc manpage</a> should in
theory provide (read-only) access to entries in the shadow password
file. To change the file, make a new shadow password file (the format
varies from system to system--see <em>passwd</em> for specifics) and use
<code>pwd_mkdb(8)</code> to install it (see <em>pwd_mkdb</em> for more details).</p>
<p>
</p>
<h2><a name="how_do_i_set_the_time_and_date">How do I set the time and date?</a></h2>
<p>Assuming you're running under sufficient permissions, you should be
able to set the system-wide date and time by running the <code>date(1)</code>
program. (There is no way to set the time and date on a per-process
basis.) This mechanism will work for Unix, MS-DOS, Windows, and NT;
the VMS equivalent is <code>set time</code>.</p>
<p>However, if all you want to do is change your time zone, you can
probably get away with setting an environment variable:</p>
<pre>
<span class="variable">$ENV</span><span class="operator">{</span><span class="string">TZ</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">"MST7MDT"</span><span class="operator">;</span> <span class="comment"># unixish</span>
<span class="variable">$ENV</span><span class="operator">{</span><span class="string">'SYS$TIMEZONE_DIFFERENTIAL'</span><span class="operator">}</span><span class="operator">=</span><span class="string">"-5"</span> <span class="comment"># vms</span>
<span class="keyword">system</span> <span class="string">"trn comp.lang.perl.misc"</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="how_can_i_sleep___or_alarm___for_under_a_second">How can I <a href="../../lib/Pod/perlfunc.html#item_sleep"><code>sleep()</code></a> or <a href="../../lib/Pod/perlfunc.html#item_alarm"><code>alarm()</code></a> for under a second?</a></h2>
<p>If you want finer granularity than the 1 second that the <a href="../../lib/Pod/perlfunc.html#item_sleep"><code>sleep()</code></a>
function provides, the easiest way is to use the <a href="../../lib/Pod/perlfunc.html#item_select"><code>select()</code></a> function as
documented in <a href="../../lib/Pod/perlfunc.html#select">select in the perlfunc manpage</a>. Try the Time::HiRes and
the BSD::Itimer modules (available from CPAN, and starting from
Perl 5.8 Time::HiRes is part of the standard distribution).</p>
<p>
</p>
<h2><a name="how_can_i_measure_time_under_a_second">How can I measure time under a second?</a></h2>
<p>In general, you may not be able to. The Time::HiRes module (available
from CPAN, and starting from Perl 5.8 part of the standard distribution)
provides this functionality for some systems.</p>
<p>If your system supports both the <a href="../../lib/Pod/perlfunc.html#item_syscall"><code>syscall()</code></a> function in Perl as well as
a system call like gettimeofday(2), then you may be able to do
something like this:</p>
<pre>
<span class="keyword">require</span> <span class="string">'sys/syscall.ph'</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$TIMEVAL_T</span> <span class="operator">=</span> <span class="string">"LL"</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$done</span> <span class="operator">=</span> <span class="variable">$start</span> <span class="operator">=</span> <span class="keyword">pack</span><span class="operator">(</span><span class="variable">$TIMEVAL_T</span><span class="operator">,</span> <span class="operator">());</span>
</pre>
<pre>
<span class="keyword">syscall</span><span class="operator">(&</span><span class="variable">SYS_gettimeofday</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="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="comment">##########################</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?