📄 fileaccess.mht
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: File Access
Date: Wed, 29 Apr 2009 09:26:28 +0800
MIME-Version: 1.0
Content-Type: text/html;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/fileaccess.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>File Access</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.5659" name=3DGENERATOR><LINK =
title=3DPLEAC-Perl=20
href=3D"index.html" rel=3DHOME><LINK title=3D"Pattern Matching"=20
href=3D"patternmatching.html" rel=3DPREVIOUS><LINK title=3D"File =
Contents"=20
href=3D"filecontents.html" rel=3DNEXT></HEAD>
<BODY class=3DSECT1 text=3D#cecece vLink=3D#d5ae83 aLink=3D#0000ff =
link=3D#f5deb3=20
bgColor=3D#4f6f6f>
<DIV class=3DNAVHEADER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%"=20
summary=3D"Header navigation table" border=3D0>
<TBODY>
<TR>
<TH align=3Dmiddle colSpan=3D3>PLEAC-Perl</TH></TR>
<TR>
<TD vAlign=3Dbottom align=3Dleft width=3D"10%"><A accessKey=3DP=20
=
href=3D"http://pleac.sourceforge.net/pleac_perl/patternmatching.html">Pre=
v</A></TD>
<TD vAlign=3Dbottom align=3Dmiddle width=3D"80%"></TD>
<TD vAlign=3Dbottom align=3Dright width=3D"10%"><A accessKey=3DN=20
=
href=3D"http://pleac.sourceforge.net/pleac_perl/filecontents.html">Next</=
A></TD></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DFILEACCESS>7. File Access</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN359>Introduction</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>INPUT, <FONT color=3D#00cd00>"< =
/usr/local/widgets/data"</FONT><FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open /usr/local/widgets/data for =
reading: $!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><INPUT><FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#ffa500>if</FONT> =
/blue/<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>INPUT<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$var =3D *STDIN<FONT color=3D#00ffff>;</FONT>
mysub<FONT color=3D#00ffff>(</FONT>$var, *LOGFILE<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>IO::File</FONT><FONT color=3D#00ffff>;</FONT>
$input =3D IO::File->new<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"< /usr/local/widgets/data"</FONT><FONT =
color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open /usr/local/widgets/data for =
reading: $!\n"</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>$line =3D =
$input->getline<FONT color=3D#00ffff>()))</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>chomp</FONT><FONT =
color=3D#00ffff>(</FONT>$line<FONT color=3D#00ffff>);</FONT>
STDOUT-><FONT color=3D#ff7f50>print</FONT><FONT =
color=3D#00ffff>(</FONT>$line<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#ffa500>if</FONT> $line =3D~ /blue/<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
$input-><FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>();</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><STDIN><FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT> <FONT color=3D#bebebe># =
reads from STDIN
</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>(</FONT>/\d/<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"No =
digit found.\n"</FONT><FONT color=3D#00ffff>;</FONT> <FONT =
color=3D#bebebe># writes to STDERR
</FONT> <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Read: =
"</FONT>, $_<FONT color=3D#00ffff>;</FONT> <FONT =
color=3D#bebebe># writes to STDOUT
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ffa500>END</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#98fb98>close</FONT><FONT color=3D#00ffff>(</FONT>STDOUT<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>or</FONT> =
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"couldn't close =
STDOUT: $!"</FONT> <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>LOGFILE, <FONT color=3D#00cd00>"> =
/tmp/log"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Can't write /tmp/log: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</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>"FH didn't close: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$old_fh =3D <FONT color=3D#98fb98>select</FONT><FONT =
color=3D#00ffff>(</FONT>LOGFILE<FONT color=3D#00ffff>);</FONT> =
<FONT color=3D#bebebe># switch to LOGFILE for output
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"Countdown initiated ...\n"</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># return to original output
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"You =
have 30 seconds to reach minimum safety distance.\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=3DAEN362>Opening 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>SOURCE, <FONT color=3D#00cd00>"< =
$path"</FONT><FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open $path for reading: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>SINK, =
<FONT color=3D#00cd00>"> $path"</FONT><FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open $path for writing: =
$!\n"</FONT><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#00ffff>;</FONT>
sysopen<FONT color=3D#00ffff>(</FONT>SOURCE, $path, O_RDONLY<FONT =
color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open $path for reading: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
sysopen<FONT color=3D#00ffff>(</FONT>SINK, $path, O_WRONLY<FONT =
color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open $path for writing: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>IO::File</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># like Perl's open
</FONT>$fh =3D IO::File->new<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"> $filename"</FONT><FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open $filename for writing: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># like Perl's sysopen
</FONT>$fh =3D IO::File->new<FONT color=3D#00ffff>(</FONT>$filename, =
O_WRONLY|O_CREAT<FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open $filename for writing: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># like stdio's fopen(3)
</FONT>$fh =3D IO::File->new<FONT color=3D#00ffff>(</FONT>$filename, =
<FONT color=3D#00cd00>"r+"</FONT><FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open $filename for read and write: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>sysopen<FONT color=3D#00ffff>(</FONT>FILEHANDLE, $name, =
$flags<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Can't open $name : $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
sysopen<FONT color=3D#00ffff>(</FONT>FILEHANDLE, $name, $flags, =
$perms<FONT color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>or</FONT> =
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Can't open $name =
: $!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FH, <FONT color=3D#00cd00>"< =
$path"</FONT><FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
$!<FONT color=3D#00ffff>;</FONT>
sysopen<FONT color=3D#00ffff>(</FONT>FH, $path, O_RDONLY<FONT =
color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> $!<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FH, <FONT color=3D#00cd00>"> =
$path"</FONT><FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
$!<FONT color=3D#00ffff>;</FONT>
sysopen<FONT color=3D#00ffff>(</FONT>FH, $path, =
O_WRONLY|O_TRUNC|O_CREAT<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> $!<FONT =
color=3D#00ffff>;</FONT>
sysopen<FONT color=3D#00ffff>(</FONT>FH, $path, =
O_WRONLY|O_TRUNC|O_CREAT, <FONT color=3D#cdcd00>0600</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> $!<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>sysopen<FONT color=3D#00ffff>(</FONT>FH, $path, =
O_WRONLY|O_EXCL|O_CREAT<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> $!<FONT =
color=3D#00ffff>;</FONT>
sysopen<FONT color=3D#00ffff>(</FONT>FH, $path, O_WRONLY|O_EXCL|O_CREAT, =
<FONT color=3D#cdcd00>0600</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> $!<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FH, <FONT color=3D#00cd00>">> =
$path"</FONT><FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
$!<FONT color=3D#00ffff>;</FONT>
sysopen<FONT color=3D#00ffff>(</FONT>FH, $path, =
O_WRONLY|O_APPEND|O_CREAT<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> $!<FONT =
color=3D#00ffff>;</FONT>
sysopen<FONT color=3D#00ffff>(</FONT>FH, $path, =
O_WRONLY|O_APPEND|O_CREAT, <FONT color=3D#cdcd00>0600</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> $!<FONT color=3D#00ffff>;</FONT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -