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

📄 filecontents.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: File Contents
Date: Wed, 29 Apr 2009 09:26:38 +0800
MIME-Version: 1.0
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/filecontents.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 Contents</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"File Access" =
href=3D"fileaccess.html"=20
rel=3DPREVIOUS><LINK title=3DDirectories href=3D"directories.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/fileaccess.html">Prev</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/directories.html">Next</A=
></TD></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DFILECONTENTS>8. File Contents</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN430>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#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>defined</FONT> <FONT =
color=3D#00ffff>(</FONT>$line =3D &lt;DATAFILE&gt;<FONT =
color=3D#00ffff>))</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>chomp</FONT> $line<FONT =
color=3D#00ffff>;</FONT>
    $size =3D <FONT color=3D#98fb98>length</FONT> $line<FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"$size\n"</FONT><FONT color=3D#00ffff>;</FONT>           =
     <FONT color=3D#bebebe># output size of line
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;DATAFILE&gt;<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>chomp</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#98fb98>length</FONT>, <FONT color=3D#00cd00>"\n"</FONT><FONT =
color=3D#00ffff>;</FONT>             <FONT color=3D#bebebe># output size =
of line
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdad00>@lines</FONT> =3D &lt;DATAFILE&gt;<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>undef</FONT> $/<FONT =
color=3D#00ffff>;</FONT>
$whole_file =3D &lt;FILE&gt;<FONT color=3D#00ffff>;</FONT>               =
<FONT color=3D#bebebe># 'slurp' mode
#-----------------------------
#% perl -040 -e '$word =3D &lt;&gt;; print "First word is $word\n";'
#-----------------------------
#% perl -ne 'BEGIN { $/=3D"%%\n" } chomp; print if /Unix/i' fortune.dat
#-----------------------------
</FONT><FONT color=3D#ff7f50>print</FONT> HANDLE <FONT =
color=3D#00cd00>"One"</FONT>, <FONT color=3D#00cd00>"two"</FONT>, <FONT =
color=3D#00cd00>"three"</FONT><FONT color=3D#00ffff>;</FONT> <FONT =
color=3D#bebebe># "Onetwothree"
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Baa baa =
black sheep.\n"</FONT><FONT color=3D#00ffff>;</FONT>     <FONT =
color=3D#bebebe># Sent to default output handle
#-----------------------------
</FONT>$rv =3D <FONT color=3D#98fb98>read</FONT><FONT =
color=3D#00ffff>(</FONT>HANDLE, $buffer, <FONT =
color=3D#cdcd00>4096</FONT><FONT color=3D#00ffff>)</FONT>
        <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't read from HANDLE : $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># $rv is the number of bytes read,
# $buffer holds the data read
#-----------------------------
</FONT><FONT color=3D#98fb98>truncate</FONT><FONT =
color=3D#00ffff>(</FONT>HANDLE, $length<FONT color=3D#00ffff>)</FONT>
    <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't truncate: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>truncate</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"/tmp/$$.pid"</FONT>, $length<FONT =
color=3D#00ffff>)</FONT>
    <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't truncate: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$pos =3D <FONT color=3D#98fb98>tell</FONT><FONT =
color=3D#00ffff>(</FONT>DATAFILE<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"I'm $pos bytes =
from the start of DATAFILE.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>seek</FONT><FONT =
color=3D#00ffff>(</FONT>LOGFILE, <FONT color=3D#cdcd00>0</FONT>, <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>"Couldn't seek to the end: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>seek</FONT><FONT color=3D#00ffff>(</FONT>DATAFILE, =
$pos, <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>"Couldn't seek to $pos: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>seek</FONT><FONT color=3D#00ffff>(</FONT>OUT, =
<FONT color=3D#cdcd00>-20</FONT>, <FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>)</FONT>           <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't seek back 20 =
bytes: $!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$written =3D <FONT color=3D#98fb98>syswrite</FONT><FONT =
color=3D#00ffff>(</FONT>DATAFILE, $mystring, <FONT =
color=3D#98fb98>length</FONT><FONT =
color=3D#00ffff>(</FONT>$mystring<FONT color=3D#00ffff>));</FONT>
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"syswrite failed: =
$!\n"</FONT> <FONT color=3D#ffa500>unless</FONT> $written =3D=3D <FONT =
color=3D#98fb98>length</FONT><FONT =
color=3D#00ffff>(</FONT>$mystring<FONT color=3D#00ffff>);</FONT>
$read =3D <FONT color=3D#98fb98>sysread</FONT><FONT =
color=3D#00ffff>(</FONT>INFILE, $block, <FONT =
color=3D#cdcd00>256</FONT>, <FONT color=3D#cdcd00>5</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"only read $read =
bytes, not 256"</FONT> <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#cdcd00>256</FONT> !=3D $read<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$pos =3D sysseek<FONT color=3D#00ffff>(</FONT>HANDLE, <FONT =
color=3D#cdcd00>0</FONT>, <FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>);</FONT>       <FONT color=3D#bebebe># don't change =
position
</FONT><FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Couldn't =
sysseek: $!\n"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#ff7f50>defined</FONT> $pos<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=3DAEN433>Reading Lines with Continuation=20
Characters</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><FONT color=3D#ff7f50>defined</FONT><FONT =
color=3D#00ffff>(</FONT>$line =3D &lt;FH&gt;<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>chomp</FONT> $line<FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>$line =
=3D~ <FONT color=3D#ff7f50>s</FONT>/\\$//<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
        $line .=3D &lt;FH&gt;<FONT color=3D#00ffff>;</FONT>
        <FONT color=3D#ffa500>redo</FONT> <FONT =
color=3D#ffa500>unless</FONT> <FONT color=3D#98fb98>eof</FONT><FONT =
color=3D#00ffff>(</FONT>FH<FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#00ffff>}</FONT>
    <FONT color=3D#bebebe># process full record in $line here
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
# DISTFILES =3D $(DIST_COMMON) $(SOURCES) $(HEADERS) \
#         $(TEXINFOS) $(INFOS) $(MANS) $(DATA)
# DEP_DISTFILES =3D $(DIST_COMMON) $(SOURCES) $(HEADERS) \
#         $(TEXINFOS) $(INFO_DEPS) $(MANS) $(DATA) \
#         $(EXTRA_DIST)
#-----------------------------
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$line =3D~ <FONT =
color=3D#ff7f50>s</FONT>/\\\s*$//<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT><FONT color=3D#43cd80> </FONT>
    <FONT color=3D#bebebe># as before
</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=3DAEN436>Counting Lines (or Paragraphs or =
Records) in a=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>$count =3D <FONT color=3D#00cd00>`wc -l &lt; $file`</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"wc failed: =
$?"</FONT> <FONT color=3D#ffa500>if</FONT> $?<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>chomp</FONT><FONT =
color=3D#00ffff>(</FONT>$count<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FILE, <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>"can't open $file: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
$count++ <FONT color=3D#ffa500>while</FONT> &lt;FILE&gt;<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># $count now holds the number of lines read
#-----------------------------
</FONT>$count +=3D <FONT color=3D#ff7f50>tr</FONT>/\n/\n/ <FONT =
color=3D#ffa500>while</FONT> <FONT color=3D#98fb98>sysread</FONT><FONT =
color=3D#00ffff>(</FONT>FILE, $_, <FONT color=3D#cdcd00>2</FONT> ** =
<FONT color=3D#cdcd00>16</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FILE, <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>"can't open $file: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
$count++ <FONT color=3D#ffa500>while</FONT> &lt;FILE&gt;<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># $count now holds the number of lines read
#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FILE, <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>"can't open $file: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>for</FONT> <FONT =
color=3D#00ffff>(</FONT>$count=3D<FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>;</FONT> &lt;FILE&gt;<FONT color=3D#00ffff>;</FONT> =
$count++<FONT color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT> =
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdcd00>1</FONT> <FONT color=3D#ffa500>while</FONT> =
&lt;FILE&gt;<FONT color=3D#00ffff>;</FONT>
$count =3D $.<FONT color=3D#00ffff>;</FONT>

⌨️ 快捷键说明

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