⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processmanagementandcommunication.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: Process Management and Communication
Date: Wed, 29 Apr 2009 09:27:46 +0800
MIME-Version: 1.0
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/processmanagementetc.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>Process Management and Communication</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.5659" name=3DGENERATOR><LINK =
title=3DPLEAC-Perl=20
href=3D"index.html" rel=3DHOME><LINK title=3D"User Interfaces"=20
href=3D"userinterfaces.html" rel=3DPREVIOUS><LINK title=3DSockets =
href=3D"sockets.html"=20
rel=3DNEXT></HEAD>
<BODY class=3DSECT1 text=3D#cecece vLink=3D#d5ae83 aLink=3D#0000ff =
link=3D#f5deb3=20
bgColor=3D#4f6f6f>
<DIV class=3DNAVHEADER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%"=20
summary=3D"Header navigation table" border=3D0>
  <TBODY>
  <TR>
    <TH align=3Dmiddle colSpan=3D3>PLEAC-Perl</TH></TR>
  <TR>
    <TD vAlign=3Dbottom align=3Dleft width=3D"10%"><A accessKey=3DP=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/userinterfaces.html">Prev=
</A></TD>
    <TD vAlign=3Dbottom align=3Dmiddle width=3D"80%"></TD>
    <TD vAlign=3Dbottom align=3Dright width=3D"10%"><A accessKey=3DN=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/sockets.html">Next</A></T=
D></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DPROCESSMANAGEMENTETC>16. Process Management =
and=20
Communication</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN851>Gathering Output from a =
Program</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT>$output =3D <FONT color=3D#00cd00>`program args`</FONT><FONT =
color=3D#00ffff>;</FONT>   <FONT color=3D#bebebe># collect output into =
one multiline string
</FONT><FONT color=3D#cdad00>@output</FONT> =3D <FONT =
color=3D#00cd00>`program args`</FONT><FONT color=3D#00ffff>;</FONT>   =
<FONT color=3D#bebebe># collect output into array, one line per element
#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>README, <FONT color=3D#00cd00>"program args =
|"</FONT><FONT color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>or</FONT> =
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Can't run =
program: $!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT><FONT =
color=3D#00ffff>(</FONT>&lt;README&gt;<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
    $output .=3D $_<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>README<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#00cd00>`fsck -y /dev/rsd1a`</FONT><FONT =
color=3D#00ffff>;</FONT>       <FONT color=3D#bebebe># BAD AND SCARY
#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>POSIX</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>:sys_<FONT color=3D#98fb98>wait</FONT>_h<FONT =
color=3D#00ffff>);</FONT>

<FONT color=3D#98fb98>pipe</FONT><FONT color=3D#00ffff>(</FONT>README, =
WRITEME<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>$pid =3D =
<FONT color=3D#98fb98>fork</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># parent
</FONT>    <FONT color=3D#cdcd00>$SIG</FONT><FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>CHLD</FONT><FONT =
color=3D#00ffff>}</FONT> =3D <FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#cdcd00>1</FONT> <FONT =
color=3D#ffa500>while</FONT> <FONT color=3D#00ffff>(</FONT> <FONT =
color=3D#98fb98>waitpid</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>-1</FONT>, WNOHANG<FONT color=3D#00ffff>))</FONT> &gt; =
<FONT color=3D#cdcd00>0</FONT> <FONT color=3D#00ffff>};</FONT>
    <FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>WRITEME<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"cannot fork: =
$!"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#ff7f50>defined</FONT> $pid<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#bebebe># child
</FONT>    <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>STDOUT, <FONT =
color=3D#00cd00>"&gt;&amp;=3DWRITEME"</FONT><FONT =
color=3D#00ffff>)</FONT>      <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't redirect =
STDOUT: $!"</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>README<FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#ffa500>exec</FONT><FONT =
color=3D#00ffff>(</FONT>$program, $arg1, $arg2<FONT =
color=3D#00ffff>)</FONT>    <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't run $program =
: $!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;README&gt;<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
    $string .=3D $_<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#bebebe># or  push(</FONT><FONT =
color=3D#cdad00>@strings</FONT><FONT color=3D#bebebe>, $_);
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>README<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN854>Running Another Program</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT>$status =3D <FONT color=3D#98fb98>system</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"vi $myfile"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$status =3D <FONT color=3D#98fb98>system</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"vi"</FONT>, $myfile<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>system</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"cmd1 args | cmd2 | cmd3 =
&gt;outfile"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#98fb98>system</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"cmd args &lt;infile &gt;outfile =
2&gt;errfile"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$status =3D <FONT color=3D#98fb98>system</FONT><FONT =
color=3D#00ffff>(</FONT>$program, $arg1, $arg<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"$program exited =
funny: $?"</FONT> <FONT color=3D#ffa500>unless</FONT> $status =3D=3D =
<FONT color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>((</FONT>$signo =3D <FONT =
color=3D#98fb98>system</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@arglist</FONT><FONT color=3D#00ffff>))</FONT> &amp;=3D =
<FONT color=3D#cdcd00>127</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT><FONT color=3D#43cd80> </FONT>
    <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"program =
killed by signal $signo\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$pid =3D <FONT color=3D#98fb98>fork</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># parent catches INT and berates user
</FONT>    <FONT color=3D#ffa500>local</FONT> <FONT =
color=3D#cdcd00>$SIG</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>INT</FONT><FONT color=3D#00ffff>}</FONT> =3D <FONT =
color=3D#ffa500>sub</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Tsk tsk, no process =
interruptus\n"</FONT> <FONT color=3D#00ffff>};</FONT>
    <FONT color=3D#98fb98>waitpid</FONT><FONT =
color=3D#00ffff>(</FONT>$pid, <FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"cannot fork: =
$!"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#ff7f50>defined</FONT> $pid<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#bebebe># child ignores INT and does its thing
</FONT>    <FONT color=3D#cdcd00>$SIG</FONT><FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>INT</FONT><FONT =
color=3D#00ffff>}</FONT> =3D <FONT color=3D#00cd00>"IGNORE"</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>exec</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"summarize"</FONT>, <FONT =
color=3D#00cd00>"/etc/logfiles"</FONT><FONT color=3D#00ffff>)</FONT>     =
        <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Can't exec: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$shell =3D <FONT color=3D#00cd00>'/bin/tcsh'</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>system</FONT> $shell <FONT =
color=3D#00cd00>'-csh'</FONT><FONT color=3D#00ffff>;</FONT>           =
<FONT color=3D#bebebe># pretend it's a login shell
#-----------------------------
</FONT><FONT color=3D#98fb98>system</FONT> <FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>'/bin/tcsh'</FONT><FONT =
color=3D#00ffff>}</FONT> <FONT color=3D#00cd00>'-csh'</FONT><FONT =
color=3D#00ffff>;</FONT>    <FONT color=3D#bebebe># pretend it's a login =
shell
#-----------------------------
# call expn as vrfy
</FONT><FONT color=3D#98fb98>system</FONT> <FONT =
color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>'/home/tchrist/scripts/expn'</FONT><FONT =
color=3D#00ffff>}</FONT> <FONT color=3D#00cd00>'vrfy'</FONT>, <FONT =
color=3D#cdad00>@ADDRESSES</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>@args</FONT> =3D <FONT =
color=3D#00ffff>(</FONT> <FONT color=3D#00cd00>"echo surprise"</FONT> =
<FONT color=3D#00ffff>);</FONT>

<FONT color=3D#98fb98>system</FONT> <FONT =
color=3D#cdad00>@args</FONT><FONT color=3D#00ffff>;</FONT>               =
<FONT color=3D#bebebe># subject to shell escapes if </FONT><FONT =
color=3D#cdad00>@args</FONT><FONT color=3D#bebebe> =3D=3D 1
</FONT><FONT color=3D#98fb98>system</FONT> <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#cdad00>$args</FONT><FONT =
color=3D#00ffff>[</FONT><FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>]</FONT> <FONT color=3D#00ffff>}</FONT> <FONT =
color=3D#cdad00>@args</FONT><FONT color=3D#00ffff>;</FONT>  <FONT =
color=3D#bebebe># safe even with one-arg list
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN857>Replacing the Current Program with a =
Different=20
One</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>exec</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"archive =
*.data"</FONT><FONT color=3D#00ffff>)</FONT>
    <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't replace myself with archive: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>exec</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"archive"</FONT>, <FONT =
color=3D#00cd00>"accounting.data"</FONT><FONT color=3D#00ffff>)</FONT>
    <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't replace myself with archive: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>exec</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"archive =
accounting.data"</FONT><FONT color=3D#00ffff>)</FONT>

⌨️ 快捷键说明

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