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

📄 fileaccess.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
color=3D#ff7f50>s</FONT> DATA<FONT color=3D#00ffff>;</FONT>
$kilosize =3D <FONT color=3D#98fb98>int</FONT><FONT =
color=3D#00ffff>(</FONT>$size / <FONT color=3D#cdcd00>1024</FONT><FONT =
color=3D#00ffff>)</FONT> . <FONT color=3D#00cd00>'k'</FONT><FONT =
color=3D#00ffff>;</FONT>

<FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"&lt;P&gt;Script size is $kilosize\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> strftime<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"&lt;P&gt;Last script =
update: </FONT><FONT color=3D#cdcd00>%c</FONT><FONT color=3D#00cd00> =
(</FONT><FONT color=3D#cdcd00>%Z</FONT><FONT =
color=3D#00cd00>)\n"</FONT>, <FONT color=3D#98fb98>localtime</FONT><FONT =
color=3D#00ffff>(</FONT>$raw_<FONT color=3D#98fb98>time</FONT><FONT =
color=3D#00ffff>));</FONT>

<FONT color=3D#bebebe>#__DATA__
#DO NOT REMOVE THE PRECEDING LINE.
#Everything else in this file will be ignored.
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN380>Writing a Filter</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>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># do something with the line
</FONT><FONT color=3D#00ffff>}</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#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>unshift</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@ARGV</FONT>, <FONT =
color=3D#00cd00>'-'</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#ffa500>unless</FONT> <FONT color=3D#cdad00>@ARGV</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT color=3D#00ffff>(</FONT>$ARGV =
=3D <FONT color=3D#ff7f50>shift</FONT> <FONT =
color=3D#cdad00>@ARGV</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>ARGV, $ARGV<FONT color=3D#00ffff>))</FONT> <FONT =
color=3D#00ffff>{</FONT>
        <FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"Can't =
open $ARGV: $!\n"</FONT><FONT color=3D#00ffff>;</FONT>
        <FONT color=3D#ffa500>next</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
    <FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>defined</FONT><FONT =
color=3D#00ffff>(</FONT>$_ =3D &lt;ARGV&gt;<FONT =
color=3D#00ffff>))</FONT> <FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#bebebe># ...
</FONT>    <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>@ARGV</FONT> =3D <FONT =
color=3D#98fb98>glob</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"*.[Cch]"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#ffa500>unless</FONT> <FONT color=3D#cdad00>@ARGV</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
# arg demo 1: Process optional -c flag</FONT><FONT color=3D#43cd80> =
</FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@ARGV</FONT> &amp;&amp; =
<FONT color=3D#cdad00>$ARGV</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>]</FONT> <FONT =
color=3D#98fb98>eq</FONT> <FONT color=3D#00cd00>'-c'</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT><FONT =
color=3D#43cd80> </FONT>
    $chop_first++<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe># arg demo 2: Process optional -NUMBER =
flag</FONT><FONT color=3D#43cd80>    </FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@ARGV</FONT> &amp;&amp; =
<FONT color=3D#cdad00>$ARGV</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>]</FONT> =3D~ /^-<FONT =
color=3D#00ffff>(</FONT>\d+<FONT color=3D#00ffff>)</FONT>$/<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT><FONT =
color=3D#43cd80> </FONT>
    $columns =3D $<FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>;</FONT><FONT color=3D#43cd80> </FONT>
    <FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe># arg demo 3: Process clustering -a, -i, -n, or -u =
flags</FONT><FONT color=3D#43cd80>     </FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@ARGV</FONT> &amp;&amp; =
<FONT color=3D#cdad00>$ARGV</FONT><FONT color=3D#00ffff>[</FONT><FONT =
color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>]</FONT> =3D~ /^-<FONT =
color=3D#00ffff>(</FONT>.+<FONT color=3D#00ffff>)</FONT>/ &amp;&amp; =
<FONT color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>shift</FONT>, <FONT =
color=3D#00ffff>(</FONT>$_ =3D $<FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>)</FONT>, <FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>))</FONT> <FONT color=3D#00ffff>{</FONT><FONT =
color=3D#43cd80> </FONT>
    <FONT color=3D#ffa500>next</FONT> <FONT color=3D#ffa500>if</FONT> =
/^$/<FONT color=3D#00ffff>;</FONT><FONT color=3D#43cd80> </FONT>
    <FONT color=3D#ff7f50>s</FONT>/a// &amp;&amp; <FONT =
color=3D#00ffff>(</FONT>++$append,      <FONT =
color=3D#ffa500>redo</FONT><FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#ff7f50>s</FONT>/i// &amp;&amp; <FONT =
color=3D#00ffff>(</FONT>++$ignore_ints, <FONT =
color=3D#ffa500>redo</FONT><FONT color=3D#00ffff>);</FONT><FONT =
color=3D#43cd80> </FONT>
    <FONT color=3D#ff7f50>s</FONT>/n// &amp;&amp; <FONT =
color=3D#00ffff>(</FONT>++$nostdout,    <FONT =
color=3D#ffa500>redo</FONT><FONT color=3D#00ffff>);</FONT><FONT =
color=3D#43cd80> </FONT>
    <FONT color=3D#ff7f50>s</FONT>/u// &amp;&amp; <FONT =
color=3D#00ffff>(</FONT>++$unbuffer,    <FONT =
color=3D#ffa500>redo</FONT><FONT color=3D#00ffff>);</FONT><FONT =
color=3D#43cd80> </FONT>
    <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"usage: $0 =
[-ainu] [filenames] ...\n"</FONT><FONT color=3D#00ffff>;</FONT><FONT =
color=3D#43cd80>    </FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>undef</FONT> $/<FONT =
color=3D#00ffff>;</FONT><FONT color=3D#43cd80>		     </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#43cd80> 	</FONT>
    <FONT color=3D#bebebe># $_ now has the complete contents =
of</FONT><FONT color=3D#43cd80> 	</FONT><FONT color=3D#bebebe>
</FONT>    <FONT color=3D#bebebe># the file whose name is in =
$ARGV</FONT><FONT color=3D#43cd80>     </FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#00ffff>{</FONT>     <FONT color=3D#bebebe># create =
block for local</FONT><FONT color=3D#43cd80> 	</FONT><FONT =
color=3D#bebebe>
</FONT>    <FONT color=3D#ffa500>local</FONT> $/<FONT =
color=3D#00ffff>;</FONT>         <FONT color=3D#bebebe># record =
separator now undef</FONT><FONT color=3D#43cd80> 	</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#43cd80> 	    </FONT>
        <FONT color=3D#bebebe># do something; called functions still =
have</FONT><FONT color=3D#43cd80> 	    </FONT><FONT color=3D#bebebe>
</FONT>        <FONT color=3D#bebebe># undeffed version of =
$/</FONT><FONT color=3D#43cd80> 	</FONT><FONT color=3D#bebebe>
</FONT>    <FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80>     =
</FONT>
<FONT color=3D#00ffff>}</FONT>                     <FONT =
color=3D#bebebe># $/ restored here
#-----------------------------
</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#43cd80> 	</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"$ARGV:$.:$_"</FONT><FONT color=3D#00ffff>;</FONT><FONT =
color=3D#43cd80> 	</FONT>
    <FONT color=3D#98fb98>close</FONT> ARGV <FONT =
color=3D#ffa500>if</FONT> <FONT color=3D#98fb98>eof</FONT><FONT =
color=3D#00ffff>;</FONT><FONT color=3D#43cd80>     </FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch07/findlogin1">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl</FONT><FONT color=3D#43cd80>   </FONT><FONT =
color=3D#bebebe>
# findlogin1 - print all lines containing the string "login"</FONT><FONT =
color=3D#43cd80>   </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># loop over files on =
command line</FONT><FONT color=3D#43cd80> 	</FONT><FONT color=3D#bebebe>
</FONT>    <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#ffa500>if</FONT> /login/<FONT color=3D#00ffff>;</FONT><FONT =
color=3D#43cd80>     </FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch07/findlogin2">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl -n</FONT><FONT color=3D#43cd80>     </FONT><FONT =
color=3D#bebebe>
# findlogin2 - print all lines containing the string "login"</FONT><FONT =
color=3D#43cd80>     </FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#ffa500>if</FONT> /login/<FONT color=3D#00ffff>;</FONT>

<FONT color=3D#bebebe>#-----------------------------
#% perl -ne 'print if /login/'
#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch07/lowercase1">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl
# lowercase - turn all lines into lowercase
</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>locale</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># loop =
over lines on command line
</FONT>    <FONT color=3D#ff7f50>s</FONT>/<FONT =
color=3D#00ffff>([</FONT>^\W0<FONT color=3D#cdcd00>-9</FONT>_<FONT =
color=3D#00ffff>])</FONT>/\l$<FONT color=3D#cdcd00>1</FONT>/g<FONT =
color=3D#00ffff>;</FONT>    <FONT color=3D#bebebe># change all letters =
to lowercase
</FONT>    <FONT color=3D#ff7f50>print</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch07/lowercase2">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl -p</FONT><FONT color=3D#43cd80>     </FONT><FONT =
color=3D#bebebe>
# lowercase - turn all lines into lowercase</FONT><FONT color=3D#43cd80> =
    </FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>locale</FONT><FONT color=3D#00ffff>;</FONT><FONT =
color=3D#43cd80>     </FONT>
<FONT color=3D#ff7f50>s</FONT>/<FONT color=3D#00ffff>([</FONT>^\W0<FONT =
color=3D#cdcd00>-9</FONT>_<FONT color=3D#00ffff>])</FONT>/\l$<FONT =
color=3D#cdcd00>1</FONT>/g<FONT color=3D#00ffff>;</FONT>	<FONT =
color=3D#bebebe># change all letters to lowercase
</FONT>
<FONT color=3D#bebebe>#-----------------------------
#% perl -Mlocale -pe 's/([^\W0-9_])/\l$1/g'
#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch07/countchunks">downl=
oad the following standalone program</A></FONT>
#!/usr/bin/perl -n</FONT><FONT color=3D#43cd80>    </FONT><FONT =
color=3D#bebebe>
# countchunks - count how many words are used.</FONT><FONT =
color=3D#43cd80>    </FONT><FONT color=3D#bebebe>
# skip comments, and bail on file if _     _END_     _</FONT><FONT =
color=3D#43cd80>   </FONT><FONT color=3D#bebebe>
# or _     _DATA_     _ seen.</FONT><FONT color=3D#43cd80>    =
</FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#ffa500>for</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>split</FONT> /\W+/<FONT =

⌨️ 快捷键说明

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