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

📄 fileaccess.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
    <FONT color=3D#ff7f50>s</FONT>/DATE/<FONT =
color=3D#98fb98>localtime</FONT>/eg<FONT color=3D#00ffff>;</FONT>
    $out .=3D $_<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#98fb98>seek</FONT><FONT color=3D#00ffff>(</FONT>F, <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>"can't seek to =
start of $infile: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> F $out                <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't print to $infile: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>truncate</FONT><FONT color=3D#00ffff>(</FONT>F, =
<FONT color=3D#98fb98>tell</FONT><FONT color=3D#00ffff>(</FONT>F<FONT =
color=3D#00ffff>))</FONT>        <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't truncate =
$infile: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT><FONT color=3D#00ffff>(</FONT>F<FONT =
color=3D#00ffff>)</FONT>                    <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't close $infile: $!"</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=3DAEN392>Locking a 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; =
$path"</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 $path: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>flock</FONT><FONT color=3D#00ffff>(</FONT>FH, =
<FONT color=3D#cdcd00>2</FONT><FONT color=3D#00ffff>)</FONT>             =
           <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't flock $path: =
$!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># update file, then...
</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>"can't close $path: =
$!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>LOCK_SH</FONT><FONT color=3D#00ffff>()</FONT>  <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#cdcd00>1</FONT> <FONT =
color=3D#00ffff>}</FONT>     <FONT color=3D#bebebe>#  Shared lock (for =
reading)
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>LOCK_EX</FONT><FONT color=3D#00ffff>()</FONT>  <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#cdcd00>2</FONT> <FONT =
color=3D#00ffff>}</FONT>     <FONT color=3D#bebebe>#  Exclusive lock =
(for writing)
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>LOCK_NB</FONT><FONT color=3D#00ffff>()</FONT>  <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#cdcd00>4</FONT> <FONT =
color=3D#00ffff>}</FONT>     <FONT color=3D#bebebe>#  Non-blocking =
request (don't stall)
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>LOCK_UN</FONT><FONT color=3D#00ffff>()</FONT>  <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#cdcd00>8</FONT> <FONT =
color=3D#00ffff>}</FONT>     <FONT color=3D#bebebe>#  Free the lock =
(careful!)
#-----------------------------
</FONT><FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>flock</FONT><FONT =
color=3D#00ffff>(</FONT>FH, LOCK_EX|LOCK_NB<FONT =
color=3D#00ffff>))</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"can't =
immediately write-lock the file ($!), blocking ..."</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>flock</FONT><FONT =
color=3D#00ffff>(</FONT>FH, LOCK_EX<FONT color=3D#00ffff>))</FONT> <FONT =
color=3D#00ffff>{</FONT>
        <FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't =
get write-lock on numfile: $!"</FONT><FONT color=3D#00ffff>;</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>$<FONT color=3D#00ffff>]</FONT> &lt; <FONT =
color=3D#cdcd00>5.004</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>                   <FONT color=3D#bebebe># test =
Perl version number
</FONT>     <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$old_fh</FONT> =3D <FONT =
color=3D#98fb98>select</FONT><FONT color=3D#00ffff>(</FONT>FH<FONT =
color=3D#00ffff>);</FONT>
     <FONT color=3D#ffa500>local</FONT> $| =3D <FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>;</FONT>                  =
<FONT color=3D#bebebe># enable command buffering
</FONT>     <FONT color=3D#ffa500>local</FONT> $\ =3D <FONT =
color=3D#00cd00>''</FONT><FONT color=3D#00ffff>;</FONT>                 =
<FONT color=3D#bebebe># clear output record separator
</FONT>     <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>""</FONT><FONT color=3D#00ffff>;</FONT>                  =
    <FONT color=3D#bebebe># trigger output flush
</FONT>     <FONT color=3D#98fb98>select</FONT><FONT =
color=3D#00ffff>(</FONT>$old_fh<FONT color=3D#00ffff>);</FONT>           =
    <FONT color=3D#bebebe># restore previous filehandle
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#98fb98>flock</FONT><FONT color=3D#00ffff>(</FONT>FH, =
LOCK_UN<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Fcntl</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>:DEFAULT :<FONT =
color=3D#98fb98>flock</FONT><FONT color=3D#00ffff>);</FONT>

sysopen<FONT color=3D#00ffff>(</FONT>FH, <FONT =
color=3D#00cd00>"numfile"</FONT>, O_RDWR|O_CREAT<FONT =
color=3D#00ffff>)</FONT>
                                    <FONT color=3D#98fb98>or</FONT> =
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"can't open =
numfile: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>flock</FONT><FONT color=3D#00ffff>(</FONT>FH, =
LOCK_EX<FONT color=3D#00ffff>)</FONT>                  <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't write-lock numfile: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># Now we have acquired the lock, it's safe for I/O
</FONT>$num =3D &lt;FH&gt; || <FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>;</FONT>                   <FONT color=3D#bebebe># DO =
NOT USE "or" THERE!!
</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>"can't rewind numfile : $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>truncate</FONT><FONT color=3D#00ffff>(</FONT>FH, =
<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>"can't truncate numfile: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> FH $num+<FONT =
color=3D#cdcd00>1</FONT>, <FONT color=3D#00cd00>"\n"</FONT>              =
 <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't write numfile: $!"</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>"can't close numfile: $!"</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=3DAEN395>Flushing 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>$old_fh =3D <FONT color=3D#98fb98>select</FONT><FONT =
color=3D#00ffff>(</FONT>OUTPUT_HANDLE<FONT color=3D#00ffff>);</FONT>
$| =3D <FONT color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>select</FONT><FONT =
color=3D#00ffff>(</FONT>$old_fh<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>IO::Handle</FONT><FONT color=3D#00ffff>;</FONT>
OUTPUT_HANDLE-&gt;autoflush<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch07/seeme">download =
the following standalone program</A></FONT>
#!/usr/bin/perl -w
# seeme - demo stdio output buffering
</FONT>$| =3D <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@ARGV</FONT> &gt; <FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>);</FONT>      <FONT color=3D#bebebe># command buffered =
if arguments given
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Now you =
don't see it..."</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>sleep</FONT> <FONT color=3D#cdcd00>2</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"now you =
do\n"</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#bebebe>#-----------------------------
</FONT>    <FONT color=3D#98fb98>select</FONT><FONT =
color=3D#00ffff>((</FONT><FONT color=3D#98fb98>select</FONT><FONT =
color=3D#00ffff>(</FONT>OUTPUT_HANDLE<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>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>FileHandle</FONT><FONT color=3D#00ffff>;</FONT>

STDERR-&gt;autoflush<FONT color=3D#00ffff>;</FONT>          <FONT =
color=3D#bebebe># already unbuffered in stdio
</FONT>$filehandle-&gt;autoflush<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>IO::Handle</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># assume REMOTE_CONN is an interactive socket =
handle,
# but DISK_FILE is a handle to a regular file.
</FONT>autoflush REMOTE_CONN  <FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>;</FONT>           <FONT color=3D#bebebe># unbuffer for =
clarity
</FONT>autoflush DISK_FILE    <FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>;</FONT>           <FONT color=3D#bebebe># buffer this =
for speed
#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch07/getpcomidx">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl
# getpcomidx - fetch www.perl.com's index.html document
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>IO::Socket</FONT><FONT color=3D#00ffff>;</FONT>
$sock =3D new IO::Socket::INET <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>PeerAddr</FONT> =3D&gt; <FONT =
color=3D#00cd00>'www.perl.com'</FONT>,
                              <FONT color=3D#00cd00>PeerPort</FONT> =
=3D&gt; <FONT color=3D#00cd00>'http(80)'</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't create =
socket: $@"</FONT> <FONT color=3D#ffa500>unless</FONT> $sock<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># the library doesn't support $! setting; it uses =
$@
</FONT>
$sock-&gt;autoflush<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>);</FONT>

<FONT color=3D#bebebe># Mac *must* have \015\012\015\012 instead of \n\n =
here.
# It's a good idea for others, too, as that's the spec,
# but implementations are encouraged to accept "\cJ\cJ" too,
# and as far as we're seen, they do.
</FONT>$sock-&gt;<FONT color=3D#ff7f50>print</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"GET /index.html =
http/1.1\n\n"</FONT><FONT color=3D#00ffff>);</FONT>
$document =3D <FONT color=3D#98fb98>join</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>''</FONT>, =
$sock-&gt;getlines<FONT color=3D#00ffff>());</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"DOC IS: =
$document\n"</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABL

⌨️ 快捷键说明

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