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

📄 ch08_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
>-00</B>, Perl will set <CODECLASS="literal">$/</CODE> to <CODECLASS="literal">&quot;&quot;</CODE>. The limit of a single octal value means you can't set <CODECLASS="literal">$/</CODE> to a multibyte string, for instance, <CODECLASS="literal">&quot;%%\n&quot;</CODE> to read <EMCLASS="emphasis">fortune</EM> files. Instead, you must use a <CODECLASS="literal">BEGIN</CODE> block:</P><PRECLASS="programlisting">% perl -ne 'BEGIN { $/=&quot;%%\n&quot; } chomp; print if /Unix/i' fortune.dat</PRE><PCLASS="para">Use <CODECLASS="literal">print</CODE> to write a line or any other data. The <CODECLASS="literal">print</CODE> function writes its arguments one after another and doesn't automatically add a line or record terminator by default.</P><PRECLASS="programlisting">print HANDLE &quot;One&quot;, &quot;two&quot;, &quot;three&quot;; # &quot;Onetwothree&quot;print &quot;Baa baa black sheep.\n&quot;;     # Sent to default output handle</PRE><PCLASS="para">There is no comma between the filehandle and the data to print. If you put a comma in there, Perl gives the error message <CODECLASS="literal">&quot;No</CODE> <CODECLASS="literal">comma</CODE> <CODECLASS="literal">allowed</CODE> <CODECLASS="literal">after</CODE> <CODECLASS="literal">filehandle&quot;</CODE>. The default output handle is STDOUT. Change it with the <CODECLASS="literal">select</CODE> function. (See the introduction to <ACLASS="xref"HREF="ch07_01.htm"TITLE="File Access">Chapter 7, <CITECLASS="chapter">File Access</CITE></A>.)</P><PCLASS="para">All systems use the virtual <CODECLASS="literal">&quot;\n&quot;</CODE> to represent a line terminator, called a <EMCLASS="emphasis">newline</EM>. There is no such thing as a newline character. It is an illusion that the operating system, device drivers, C libraries, and Perl all conspire to preserve. Sometimes, this changes the number of characters in the strings you read and write. The conspiracy is revealed in <ACLASS="xref"HREF="ch08_12.htm"TITLE="Processing Binary Files">Recipe 8.11</A>.</P><PCLASS="para">Use the <CODECLASS="literal">read</CODE><ACLASS="indexterm"NAME="ch08-idx-1000004606-0"></A> function to read a <ACLASS="indexterm"NAME="ch08-idx-1000004763-0"></A><ACLASS="indexterm"NAME="ch08-idx-1000004763-1"></A><ACLASS="indexterm"NAME="ch08-idx-1000004763-2"></A>fixed-length record. It takes three arguments: a filehandle, a scalar variable, and the number of bytes to read. It returns <CODECLASS="literal">undef</CODE> if an error occurred or else the number of bytes read. To write a fixed-length record, just use <CODECLASS="literal">print</CODE>.</P><PRECLASS="programlisting">$rv = read(HANDLE, $buffer, 4096)        or die &quot;Couldn't read from HANDLE : $!\n&quot;;# $rv is the number of bytes read,# $buffer holds the data read</PRE><PCLASS="para">The <CODECLASS="literal">truncate</CODE><ACLASS="indexterm"NAME="ch08-idx-1000004607-0"></A> function changes the length of a file, which can be specified as a filehandle or as a filename. It returns true if the file was successfully truncated, false otherwise:</P><PRECLASS="programlisting">truncate(HANDLE, $length)    or die &quot;Couldn't truncate: $!\n&quot;;truncate(&quot;/tmp/$$.pid&quot;, $length)    or die &quot;Couldn't truncate: $!\n&quot;;</PRE><PCLASS="para">Each filehandle keeps track of where it is in the file. Reads and writes occur from this point, unless you've specified the <CODECLASS="literal">O_APPEND</CODE> flag (see <ACLASS="xref"HREF="ch07_02.htm"TITLE="Opening a File">Recipe 7.1</A>). Fetch the file position for a filehandle with <CODECLASS="literal">tell</CODE><ACLASS="indexterm"NAME="ch08-idx-1000004608-0"></A><ACLASS="indexterm"NAME="ch08-idx-1000004608-1"></A>, and set it with <CODECLASS="literal">seek</CODE>. Because the stdio library rewrites data to preserve the illusion that <CODECLASS="literal">&quot;\n&quot;</CODE> is the line terminator, you cannot portably <CODECLASS="literal">seek</CODE> to offsets calculated by counting characters. Instead, only <CODECLASS="literal">seek</CODE> to offsets returned by <CODECLASS="literal">tell</CODE>.</P><PRECLASS="programlisting">$pos = tell(DATAFILE);print &quot;I'm $pos bytes from the start of DATAFILE.\n&quot;;</PRE><PCLASS="para">The <CODECLASS="literal">seek</CODE> function takes three arguments: the filehandle, the offset (in bytes) to go to, and a numeric argument indicating how to interpret the offset. 0 indicates an offset from the start of the file (the kind of value returned by <CODECLASS="literal">tell</CODE>); 1, an offset from the current location (a negative number means move backwards in the file, a positive number means move forward); and 2, an offset from end of file.</P><PRECLASS="programlisting">seek(LOGFILE, 0, 2)         or die &quot;Couldn't seek to the end: $!\n&quot;;seek(DATAFILE, $pos, 0)     or die &quot;Couldn't seek to $pos: $!\n&quot;;seek(OUT, -20, 1)           or die &quot;Couldn't seek back 20 bytes: $!\n&quot;;</PRE><PCLASS="para"><ACLASS="indexterm"NAME="ch08-idx-1000004609-0"></A><ACLASS="indexterm"NAME="ch08-idx-1000004609-1"></A><ACLASS="indexterm"NAME="ch08-idx-1000004609-2"></A>So far we've been describing buffered I/O. That is, <CODECLASS="literal">&lt;FH&gt;</CODE>, <CODECLASS="literal">print</CODE>, <CODECLASS="literal">read</CODE>, <CODECLASS="literal">seek</CODE>, and <CODECLASS="literal">tell</CODE> are all operations that use buffers for speed. Perl also provides unbuffered I/O operations: <CODECLASS="literal">sysread</CODE>, <CODECLASS="literal">syswrite</CODE>, and <CODECLASS="literal">sysseek</CODE>, all discussed in <ACLASS="xref"HREF="ch07_01.htm"TITLE="File Access">Chapter 7</A>.</P><PCLASS="para">The <CODECLASS="literal">sysread</CODE><ACLASS="indexterm"NAME="ch08-idx-1000004610-0"></A><ACLASS="indexterm"NAME="ch08-idx-1000004610-1"></A> and <CODECLASS="literal">syswrite</CODE> functions are different from their <CODECLASS="literal">&lt;FH&gt;</CODE> and <CODECLASS="literal">print</CODE> counterparts. They both take a filehandle to act on, a scalar variable to either read into or write out from, and the number of bytes to read or write. They can also take an optional fourth argument, the offset in the scalar variable to start reading or writing at:</P><PRECLASS="programlisting">$written = syswrite(DATAFILE, $mystring, length($mystring));die &quot;syswrite failed: $!\n&quot; unless $written == length($mystring);$read = sysread(INFILE, $block, 256, 5);warn &quot;only read $read bytes, not 256&quot; if 256 != $read;</PRE><PCLASS="para">The <CODECLASS="literal">syswrite</CODE> call sends the contents of <CODECLASS="literal">$mystring</CODE> to <CODECLASS="literal">DATAFILE</CODE>. The <CODECLASS="literal">sysread</CODE> call reads 256 bytes from <CODECLASS="literal">INFILE</CODE> and stores them 5 characters into <CODECLASS="literal">$block</CODE>, leaving its first 5 characters intact. Both <CODECLASS="literal">sysread</CODE> and <CODECLASS="literal">syswrite</CODE> return the number of bytes transferred, which could be different than the amount of data you were attempting to transfer. Maybe the file didn't have all the data you thought it did, so you got a short read. Maybe the filesystem that the file lives on filled up. Maybe your process was interrupted part of the way through the write. Stdio takes care of finishing the transfer in cases of interruption, but if you use the <CODECLASS="literal">sysread</CODE> and <CODECLASS="literal">syswrite</CODE> calls, you must do it yourself. See <ACLASS="xref"HREF="ch09_04.htm"TITLE="Copying or Moving a File">Recipe 9.3</A> for an example of this.</P><PCLASS="para">The <CODECLASS="literal">sysseek</CODE><ACLASS="indexterm"NAME="ch08-idx-1000004611-0"></A> function doubles as an unbuffered replacement for both <CODECLASS="literal">seek</CODE> and <CODECLASS="literal">tell</CODE>. It takes the same arguments as <CODECLASS="literal">seek</CODE>, but it returns either the new position if successful or <CODECLASS="literal">undef</CODE> on error. To find the current position within the file:</P><PRECLASS="programlisting">$pos = sysseek(HANDLE, 0, 1);       # don't change positiondie &quot;Couldn't sysseek: $!\n&quot; unless defined $pos;</PRE><PCLASS="para">These are the basic operations available to you. The art and craft of programming lies in using these basic operations to solve complex problems like finding the number of lines in a file, reversing the order of lines in a file, randomly selecting a line from a file, building an index for a file, and so on.</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_23.htm"TITLE="7.22. Program: lockarea"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.22. Program: lockarea"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_02.htm"TITLE="8.1. Reading Lines with Continuation Characters"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 8.1. Reading Lines with Continuation Characters"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">7.22. Program: lockarea</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">8.1. Reading Lines with Continuation Characters</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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