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

📄 fileaccess.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT><FONT =
color=3D#43cd80> </FONT>
    <FONT color=3D#ffa500>next</FONT> <FONT color=3D#ff7f50>LINE</FONT> =
<FONT color=3D#ffa500>if</FONT> /^<FONT color=3D#bebebe>#/;</FONT><FONT =
color=3D#43cd80> </FONT><FONT color=3D#bebebe>
</FONT>    <FONT color=3D#98fb98>close</FONT> ARGV <FONT =
color=3D#ffa500>if</FONT> /_     _<FONT =
color=3D#00ffff>(</FONT>DATA|<FONT color=3D#ffa500>END</FONT><FONT =
color=3D#00ffff>)</FONT>_     _/<FONT color=3D#00ffff>;</FONT><FONT =
color=3D#43cd80> </FONT>
    $chunks++<FONT color=3D#00ffff>;</FONT><FONT color=3D#43cd80>     =
</FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80>     </FONT>
<FONT color=3D#ffa500>END</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Found $chunks =
chunks\n"</FONT> <FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe>#-----------------------------
#+0894382237
#less /etc/motd
#+0894382239
#vi ~/.exrc
#+0894382242
#date
#+0894382242
#who
#+0894382288
#telnet home
#-----------------------------
#% perl -pe 's/^#\+(\d+)\n/localtime($1) . " "/e'</FONT><FONT =
color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#Tue May  5 09:30:37 1998     less /etc/motd</FONT><FONT =
color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#
#Tue May  5 09:30:39 1998     vi ~/.exrc</FONT><FONT color=3D#43cd80> =
</FONT><FONT color=3D#bebebe>
#
#Tue May  5 09:30:42 1998     date
#
#Tue May  5 09:30:42 1998     who</FONT><FONT color=3D#43cd80> =
</FONT><FONT color=3D#bebebe>
#
#Tue May  5 09:31:28 1998     telnet home
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN383>Modifying a File in Place with =
Temporary=20
File</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#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>OLD, <FONT color=3D#00cd00>"&lt; =
$old"</FONT><FONT color=3D#00ffff>)</FONT>         <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't open $old: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>NEW, =
<FONT color=3D#00cd00>"&gt; $new"</FONT><FONT color=3D#00ffff>)</FONT>   =
      <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"can't open $new: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;OLD&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># change $_, then...
</FONT>    <FONT color=3D#ff7f50>print</FONT> NEW $_            <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't write $new: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#98fb98>close</FONT><FONT color=3D#00ffff>(</FONT>OLD<FONT =
color=3D#00ffff>)</FONT>                  <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't close $old: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT><FONT color=3D#00ffff>(</FONT>NEW<FONT =
color=3D#00ffff>)</FONT>                  <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't close $new: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>rename</FONT><FONT color=3D#00ffff>(</FONT>$old, =
<FONT color=3D#00cd00>"$old.orig"</FONT><FONT color=3D#00ffff>)</FONT>   =
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't rename $old to $old.orig: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>rename</FONT><FONT color=3D#00ffff>(</FONT>$new, =
$old<FONT color=3D#00ffff>)</FONT>          <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't rename $new to $old: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;OLD&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>$. =
=3D=3D <FONT color=3D#cdcd00>20</FONT><FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#ff7f50>print</FONT> NEW <FONT =
color=3D#00cd00>"Extra line 1\n"</FONT><FONT color=3D#00ffff>;</FONT>
        <FONT color=3D#ff7f50>print</FONT> NEW <FONT =
color=3D#00cd00>"Extra line 2\n"</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
    <FONT color=3D#ff7f50>print</FONT> NEW $_<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;OLD&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>next</FONT> <FONT color=3D#ffa500>if</FONT> =
<FONT color=3D#cdcd00>20</FONT> .. <FONT color=3D#cdcd00>30</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ff7f50>print</FONT> NEW $_<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=3DAEN386>Modifying a File in Place with -i =
Switch</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 -i.orig -p -e 'FILTER COMMAND' file1 file2 file3 ...
#-----------------------------
#!/usr/bin/perl -i.orig -p
# filter commands go here
#-----------------------------
#% perl -pi.orig -e 's/DATE/localtime/e'
#-----------------------------
</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#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>$ARGV =
<FONT color=3D#98fb98>ne</FONT> $oldargv<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>           <FONT color=3D#bebebe># are we =
at the next file?
</FONT>        <FONT color=3D#98fb98>rename</FONT><FONT =
color=3D#00ffff>(</FONT>$ARGV, $ARGV . <FONT =
color=3D#00cd00>'.orig'</FONT><FONT color=3D#00ffff>);</FONT>
        <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>ARGVOUT, <FONT =
color=3D#00cd00>"&gt;$ARGV"</FONT><FONT color=3D#00ffff>);</FONT>       =
<FONT color=3D#bebebe># plus error check
</FONT>        <FONT color=3D#98fb98>select</FONT><FONT =
color=3D#00ffff>(</FONT>ARGVOUT<FONT color=3D#00ffff>);</FONT>
        $oldargv =3D $ARGV<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
    <FONT color=3D#ff7f50>s</FONT>/DATE/<FONT =
color=3D#98fb98>localtime</FONT>/e<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ffa500>continue</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#98fb98>select</FONT> <FONT =
color=3D#00ffff>(</FONT>STDOUT<FONT color=3D#00ffff>);</FONT>            =
          <FONT color=3D#bebebe># restore default output
#-----------------------------
#Dear Sir/Madam/Ravenous Beast,
#    As of DATE, our records show your account
#is overdue.  Please settle by the end of the month.
#Yours in cheerful usury,
#    --A. Moneylender
#-----------------------------
#Dear Sir/Madam/Ravenous Beast,
#    As of Sat Apr 25 12:28:33 1998, our records show your account
#is overdue.  Please settle by the end of the month.
#Yours in cheerful usury,
#    --A. Moneylender
#-----------------------------
#% perl -i.old -pe 's{\bhisvar\b}{</FONT><FONT =
color=3D#00cd00>hervar</FONT><FONT color=3D#bebebe>}g' *.[Cchy]
#-----------------------------
# set up to iterate over the *.c files in the current directory,
# editing in place and saving the old file with a .orig extension
</FONT><FONT color=3D#ffa500>local</FONT> $^I   =3D <FONT =
color=3D#00cd00>'.orig'</FONT><FONT color=3D#00ffff>;</FONT>             =
 <FONT color=3D#bebebe># emulate  -i.orig
</FONT><FONT color=3D#ffa500>local</FONT> <FONT =
color=3D#cdad00>@ARGV</FONT> =3D <FONT color=3D#98fb98>glob</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"*.c"</FONT><FONT =
color=3D#00ffff>);</FONT>          <FONT color=3D#bebebe># initialize =
list of files
</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#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>$. =
=3D=3D <FONT color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"This =
line should appear at the top of each file\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
    <FONT color=3D#ff7f50>s</FONT>/\b<FONT =
color=3D#00ffff>(</FONT>p<FONT color=3D#00ffff>)</FONT>earl\b/$<FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>1</FONT><FONT =
color=3D#00ffff>}</FONT>erl/ig<FONT color=3D#00ffff>;</FONT>       <FONT =
color=3D#bebebe># Correct typos, preserving case
</FONT>    <FONT color=3D#ff7f50>print</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>continue</FONT> =
<FONT color=3D#00ffff>{</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#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN389>Modifying a File in Place Without a =
Temporary=20
File</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#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FH, <FONT color=3D#00cd00>"+&lt; =
FILE"</FONT><FONT color=3D#00ffff>)</FONT>                 <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Opening: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>@ARRAY</FONT> =3D &lt;FH&gt;<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># change ARRAY here
</FONT><FONT color=3D#98fb98>seek</FONT><FONT =
color=3D#00ffff>(</FONT>FH,<FONT color=3D#cdcd00>0</FONT>,<FONT =
color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>)</FONT>                   =
     <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Seeking: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> FH <FONT =
color=3D#cdad00>@ARRAY</FONT>                     <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Printing: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>truncate</FONT><FONT =
color=3D#00ffff>(</FONT>FH,<FONT color=3D#98fb98>tell</FONT><FONT =
color=3D#00ffff>(</FONT>FH<FONT color=3D#00ffff>))</FONT>               =
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Truncating: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT><FONT color=3D#00ffff>(</FONT>FH<FONT =
color=3D#00ffff>)</FONT>                           <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Closing: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>F, =
<FONT color=3D#00cd00>"+&lt; $infile"</FONT><FONT =
color=3D#00ffff>)</FONT>       <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't read $infile: =
$!"</FONT><FONT color=3D#00ffff>;</FONT>
$out =3D <FONT color=3D#00cd00>''</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;F&gt;<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>

⌨️ 快捷键说明

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