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

📄 processmanagementandcommunication.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
    <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></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN860>Reading or Writing to 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>$pid =3D <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>README, <FONT color=3D#00cd00>"program arguments =
|"</FONT><FONT color=3D#00ffff>)</FONT>  <FONT color=3D#98fb98>or</FONT> =
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't fork: =
$!\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>
    <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#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't close: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$pid =3D <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>WRITEME, <FONT color=3D#00cd00>"| program =
arguments"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Couldn't fork: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> WRITEME <FONT =
color=3D#00cd00>"data\n"</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#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't close: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$pid =3D <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>F, <FONT color=3D#00cd00>"sleep =
100000|"</FONT><FONT color=3D#00ffff>);</FONT>    <FONT =
color=3D#bebebe># child goes to sleep
</FONT><FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>F<FONT color=3D#00ffff>);</FONT>                 =
          <FONT color=3D#bebebe># and the parent goes to lala land
#-----------------------------
</FONT>$pid =3D <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>WRITEME, <FONT color=3D#00cd00>"| program =
args"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> WRITEME <FONT =
color=3D#00cd00>"hello\n"</FONT><FONT color=3D#00ffff>;</FONT>           =
 <FONT color=3D#bebebe># program will get hello\n on STDIN
</FONT><FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>WRITEME<FONT color=3D#00ffff>);</FONT>           =
          <FONT color=3D#bebebe># program will get EOF on STDIN
#-----------------------------
</FONT>$pager =3D <FONT color=3D#cdcd00>$ENV</FONT><FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>PAGER</FONT><FONT =
color=3D#00ffff>}</FONT> || <FONT =
color=3D#00cd00>'/usr/bin/less'</FONT><FONT color=3D#00ffff>;</FONT>  =
<FONT color=3D#bebebe># XXX: might not exist
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>STDOUT, <FONT color=3D#00cd00>"| =
$pager"</FONT><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=3DAEN863>Filtering Your Own Output</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>head<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>100</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>head</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$lines</FONT> =
=3D <FONT color=3D#ff7f50>shift</FONT> || <FONT =
color=3D#cdcd00>20</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> <FONT color=3D#ffa500>if</FONT> =
$pid =3D <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>STDOUT, <FONT color=3D#00cd00>"|-"</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#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;STDIN&gt;<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#ff7f50>print</FONT><FONT color=3D#00ffff>;</FONT>
        <FONT color=3D#ffa500>last</FONT> <FONT =
color=3D#ffa500>unless</FONT> --$lines <FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
    <FONT color=3D#ffa500>exit</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdcd00>1</FONT>: &gt; Welcome to Linux, version =
<FONT color=3D#cdcd00>2.0</FONT>.<FONT color=3D#cdcd00>33</FONT> on a =
i686

<FONT color=3D#cdcd00>2</FONT>: &gt;<FONT color=3D#43cd80> </FONT>

<FONT color=3D#cdcd00>3</FONT>: &gt;     <FONT color=3D#00cd00>"The =
software required `Windows 95 or better',</FONT><FONT color=3D#43cd80> =
</FONT><FONT color=3D#00cd00>

4: &gt;      so I installed Linux."</FONT><FONT color=3D#43cd80>  =
</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>&gt; <FONT color=3D#cdcd00>1</FONT>: Welcome to Linux, Kernel =
version <FONT color=3D#cdcd00>2.0</FONT>.<FONT color=3D#cdcd00>33</FONT> =
on a i686

&gt; <FONT color=3D#cdcd00>2</FONT>:<FONT color=3D#43cd80> </FONT>

&gt; <FONT color=3D#cdcd00>3</FONT>:     <FONT color=3D#00cd00>"The =
software required `Windows 95 or better',</FONT><FONT color=3D#43cd80> =
</FONT><FONT color=3D#00cd00>

&gt; 4:      so I installed Linux."</FONT><FONT color=3D#43cd80>  =
</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch16/qnumcat">download =
the following standalone program</A></FONT>
#!/usr/bin/perl
# qnumcat - demo additive output filters
</FONT>
number<FONT color=3D#00ffff>();</FONT>                   <FONT =
color=3D#bebebe># push number filter on STDOUT
</FONT>quote<FONT color=3D#00ffff>();</FONT>                    <FONT =
color=3D#bebebe># push quote filter on STDOUT
</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>                <FONT color=3D#bebebe># act like =
/bin/cat
</FONT>    <FONT color=3D#ff7f50>print</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>

<FONT color=3D#98fb98>close</FONT> STDOUT<FONT color=3D#00ffff>;</FONT>  =
             <FONT color=3D#bebebe># tell kids we're done--politely
</FONT><FONT color=3D#ffa500>exit</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>number</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$pid</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> <FONT color=3D#ffa500>if</FONT> =
$pid =3D <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>STDOUT, <FONT color=3D#00cd00>"|-"</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#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;STDIN&gt;<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT> <FONT color=3D#ff7f50>printf</FONT> <FONT =
color=3D#00cd00>"</FONT><FONT color=3D#cdcd00>%d:</FONT><FONT =
color=3D#00cd00> </FONT><FONT color=3D#cdcd00>%s</FONT><FONT =
color=3D#00cd00>"</FONT>, $., $_ <FONT color=3D#00ffff>}</FONT><FONT =
color=3D#43cd80> </FONT>
    <FONT color=3D#ffa500>exit</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>quote</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$pid</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> <FONT color=3D#ffa500>if</FONT> =
$pid =3D <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>STDOUT, <FONT color=3D#00cd00>"|-"</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#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;STDIN&gt;<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT> <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"&gt; $_"</FONT> <FONT color=3D#00ffff>}</FONT><FONT =
color=3D#43cd80> </FONT>
    <FONT color=3D#ffa500>exit</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>

<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN866>Preprocessing Input</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#cdad00>@ARGV</FONT> =3D <FONT =
color=3D#ff7f50>map</FONT> <FONT color=3D#00ffff>{</FONT> /\.<FONT =
color=3D#00ffff>(</FONT>gz|Z<FONT color=3D#00ffff>)</FONT>$/ ? <FONT =
color=3D#00cd00>"gzip -dc $_ |"</FONT> : $_  <FONT =
color=3D#00ffff>}</FONT> <FONT color=3D#cdad00>@ARGV</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># .......
</FONT><FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>@ARGV</FONT> =3D <FONT =
color=3D#ff7f50>map</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#ff7f50>m</FONT><FONT color=3D#bebebe>#^\w+://# ? "GET $_ |" : =
$_ } </FONT><FONT color=3D#cdad00>@ARGV</FONT><FONT color=3D#bebebe>;
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># .......
</FONT><FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$pwdinfo =3D <FONT color=3D#00cd00>`domainname`</FONT> =3D~ =
/^<FONT color=3D#00ffff>(</FONT>\<FONT =
color=3D#00ffff>(</FONT>none\<FONT color=3D#00ffff>))</FONT>?$/
                ? <FONT color=3D#00cd00>'&lt; /etc/passwd'</FONT>
                : <FONT color=3D#00cd00>'ypcat  passwd |'</FONT><FONT =
color=3D#00ffff>;</FONT>

<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>PWD, =
$pwdinfo<FONT color=3D#00ffff>)</FONT>                 <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't open $pwdinfo: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"File, =
please? "</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>chomp</FONT><FONT color=3D#00ffff>(</FONT>$file =
=3D &lt;&gt;<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#98fb98>open</FONT> <FONT color=3D#00ffff>(</FONT>FH, =
$file<FONT color=3D#00ffff>)</FONT>                    <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't open $file: $!"</FONT><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=3DAEN869>Reading STDERR 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>#-----------------------------

⌨️ 快捷键说明

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