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

📄 packages, libraries, and modules.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
color=3D#00ffff>]);</FONT>
    croak <FONT color=3D#00cd00>"need open filehandle"</FONT> <FONT =
color=3D#ffa500>unless</FONT> <FONT color=3D#ff7f50>defined</FONT> <FONT =
color=3D#98fb98>fileno</FONT><FONT color=3D#00ffff>(</FONT>$handle<FONT =
color=3D#00ffff>);</FONT>

    <FONT color=3D#ff7f50>push</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@retlist</FONT>, $line<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#ff7f50>defined</FONT><FONT color=3D#00ffff>(</FONT>$line =3D =
&lt;$handle&gt;<FONT color=3D#00ffff>)</FONT> &amp;&amp; $count--<FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> <FONT =
color=3D#cdad00>@retlist</FONT><FONT color=3D#00ffff>;</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=3DAEN660>Automating Module Clean-Up</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>$Logfile =3D <FONT color=3D#00cd00>"/tmp/mylog"</FONT> <FONT =
color=3D#ffa500>unless</FONT> <FONT color=3D#ff7f50>defined</FONT> =
$Logfile<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>LF, <FONT =
color=3D#00cd00>"&gt;&gt;$Logfile"</FONT><FONT color=3D#00ffff>)</FONT>
    <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"can't append to $Logfile: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>select</FONT><FONT color=3D#00ffff>(((</FONT><FONT =
color=3D#98fb98>select</FONT><FONT color=3D#00ffff>(</FONT>LF<FONT =
color=3D#00ffff>)</FONT>, $|=3D<FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>))[</FONT><FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>]);</FONT>  <FONT color=3D#bebebe># unbuffer LF
</FONT>logmsg<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"startup"</FONT><FONT color=3D#00ffff>);</FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>logmsg</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$now</FONT> =
=3D <FONT color=3D#ff7f50>scalar</FONT> <FONT =
color=3D#98fb98>gmtime</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ff7f50>print</FONT> LF <FONT color=3D#00cd00>"$0 $$ =
$now: </FONT><FONT color=3D#cdad00>@_</FONT><FONT =
color=3D#00cd00>\n"</FONT>
        <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"write to $Logfile failed: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#ffa500>END</FONT> <FONT color=3D#00ffff>{</FONT>
    logmsg<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"shutdown"</FONT><FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>LF<FONT color=3D#00ffff>)</FONT><FONT =
color=3D#43cd80>                     </FONT>
        <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"close $Logfile failed: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>sigtrap</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ffa500>die</FONT> normal-signals =
error-signals<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=3DAEN663>Keeping Your Own Module =
Directory</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>#-----------------------------
#% perl -e 'for (</FONT><FONT color=3D#cdad00>@INC</FONT><FONT =
color=3D#bebebe>) { printf "</FONT><FONT color=3D#cdcd00>%d</FONT><FONT =
color=3D#bebebe> </FONT><FONT color=3D#cdcd00>%s</FONT><FONT =
color=3D#bebebe>\n", $i++, $_ }'
#0 /usr/local/perl/lib/i686-linux/5.004
#
#1 /usr/local/perl/lib
#
#2 /usr/local/perl/lib/site_perl/i686-linux
#
#3 /usr/local/perl/lib/site_perl
#
#4 .
#-----------------------------
# syntax for sh, bash, ksh, or zsh
#$ export PERL5LIB=3D$HOME/perllib
</FONT>
<FONT color=3D#bebebe># syntax for csh or tcsh
#% setenv PERL5LIB ~/perllib
#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>lib</FONT> =
<FONT color=3D#00cd00>"/projects/spectre/lib"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>FindBin</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>lib</FONT> =
$FindBin::Bin<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>FindBin</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>$Bin<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>lib</FONT> <FONT =
color=3D#00cd00>"$Bin/../lib"</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=3DAEN666>Preparing a Module for =
Distribution</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>#-----------------------------
#% h2xs -XA -n Planets
#% h2xs -XA -n Astronomy::Orbits
#-----------------------------
</FONT><FONT color=3D#ffa500>package</FONT> <FONT =
color=3D#b2dfee>Astronomy::Orbits</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>require</FONT> <FONT =
color=3D#b2dfee>Exporter</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>require</FONT> <FONT =
color=3D#b2dfee>AutoLoader</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>@ISA</FONT> =3D <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>Exporter =
AutoLoader<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>require</FONT> <FONT =
color=3D#b2dfee>Exporter</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>require</FONT> <FONT =
color=3D#b2dfee>DynaLoader</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>@ISA</FONT> =3D <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>Exporter =
DynaLoader<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
#% make dist
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN669>Speeding Module Loading with =
SelfLoader</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>require</FONT> <FONT =
color=3D#b2dfee>Exporter</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>require</FONT> <FONT =
color=3D#b2dfee>SelfLoader</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>@ISA</FONT> =3D <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>Exporter =
SelfLoader<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#
# other initialization or declarations here
#
#__DATA__
#sub abc { .... }
#sub def { .... }
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN672>Speeding Up Module Loading with=20
Autoloader</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>#-----------------------------
#% h2xs -Xn Sample
#% cd Sample
#% perl Makefile.PL LIB=3D~/perllib
#% (edit Sample.pm)
#% make install
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN675>Overriding Built-In =
Functions</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>package</FONT> <FONT =
color=3D#b2dfee>FineTime</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>strict</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>require</FONT> <FONT =
color=3D#b2dfee>Exporter</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>vars</FONT> <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@ISA</FONT> <FONT color=3D#cdad00>@EXPORT_OK</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#cdad00>@ISA</FONT> =3D <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>Exporter<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#cdad00>@EXPORT_OK</FONT> =3D <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>time</FONT><FONT color=3D#00ffff>);</FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#98fb98>time</FONT><FONT =
color=3D#00ffff>()</FONT> <FONT color=3D#00ffff>{</FONT> ..... <FONT =
color=3D#00ffff>}</FONT>  <FONT color=3D#bebebe># TBA
#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>FineTime</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>time</FONT><FONT =
color=3D#00ffff>);</FONT>
$start =3D <FONT color=3D#98fb98>time</FONT><FONT =
color=3D#00ffff>();</FONT>
<FONT color=3D#cdcd00>1</FONT> <FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#ff7f50>print</FONT> <FONT color=3D#98fb98>time</FONT><FONT =
color=3D#00ffff>()</FONT> - $start, <FONT =
color=3D#00cd00>"\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=3DAEN678>Reporting Errors and Warnings Like=20
Built-Ins</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>sub</FONT> <FONT =
color=3D#b2dfee>even_only</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$n</FONT> =3D =
<FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"$n is not =
even"</FONT> <FONT color=3D#ffa500>if</FONT> $n &amp; <FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>;</FONT>  <FONT =
color=3D#bebebe># one way to test
</FONT>    <FONT color=3D#bebebe>#....
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Carp</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>even_only</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$n</FONT> =3D =
<FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
    croak <FONT color=3D#00cd00>"$n is not even"</FONT> <FONT =
color=3D#ffa500>if</FONT> $n % <FONT color=3D#cdcd00>2</FONT><FONT =
color=3D#00ffff>;</FONT>  <FONT color=3D#bebebe># here's another
</FONT>    <FONT color=3D#bebebe>#....
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Carp</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>even_only</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$n</FONT> =3D =
<FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>$n =
&amp; <FONT color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>         <FONT color=3D#bebebe># test whether =
odd number
</FONT>        carp <FONT color=3D#00cd00>"$n is not even, =
continuing"</FONT><FONT color=3D#00ffff>;</FONT>
        ++$n<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
    <FONT color=3D#bebebe>#....
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>carp <FONT color=3D#00cd00>"$n is not even, continuing"</FONT> =
<FONT color=3D#ffa500>if</FONT> $^W<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=3DAEN681>Referring to Packages =

⌨️ 快捷键说明

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