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

📄 ch32_26.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>FileHandle (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="FileHandle"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch32_25.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch32_01.htm">Chapter 32: Standard Modules</a></td><td align="right" valign="top" width="172"><a href="ch32_27.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">32.26. FileHandle</h2><p><blockquote><pre class="programlisting">use FileHandle;$fh = new FileHandle;if ($fh-&gt;open("&lt; file")) {    print $line while defined($line = $fh-&gt;getline);    $fh-&gt;close;}$pos = $fh-&gt;getpos;     # like tell()$fh-&gt;setpos($pos);      # like seek()($readfh, $writefh) = FileHandle::pipe();autoflush STDOUT 1;</pre></blockquote>The <tt class="literal">FileHandle</tt> module mostly serves as a mechanism for cloakingPerl's punctuation variables in longer, more OO-looking calls.  Itis provided for compatibility with older releases, but is now reallyonly a frontend for several more specific modules, like <tt class="literal">IO::Handle</tt>and <tt class="literal">IO::File</tt>.<a href="#FOOTNOTE-5">[5]</a>Its best property is the low-level access it provides to certain rare functionsfrom the C library (<em class="emphasis">clearerr</em>(3), <em class="emphasis">fgetpos</em>(3), <em class="emphasis">fsetpos</em>(3),and <em class="emphasis">setvbuf</em>(3)).</p><blockquote class="footnote"><a name="FOOTNOTE-5"></a><p>[5]Because it loads so much code, this modulecosts you a megabyte or so of memory.</p></blockquote><a name="perl3-tab-filehandlemod"></a><table border="1"><tr><th>Variable</th><th>Method</th></tr><tr><td><tt class="literal">$|</tt></td><td><tt class="literal">autoflush</tt></td></tr><tr><td><tt class="literal">$,</tt></td><td><tt class="literal">output_field_separator</tt></td></tr><tr><td><tt class="literal">$\</tt></td><td><tt class="literal">output_record_separator</tt></td></tr><tr><td><tt class="literal">$/</tt></td><td><tt class="literal">input_record_separator</tt></td></tr><tr><td><tt class="literal">$.</tt></td><td><tt class="literal">input_line_number</tt></td></tr><tr><td><tt class="literal">$%</tt></td><td><tt class="literal">format_page_number</tt></td></tr><tr><td><tt class="literal">$=</tt></td><td><tt class="literal">format_lines_per_page</tt></td></tr><tr><td><tt class="literal">$-</tt></td><td><tt class="literal">format_lines_left</tt></td></tr><tr><td><tt class="literal">$~</tt></td><td><tt class="literal">format_name</tt></td></tr><tr><td><tt class="literal">$^</tt></td><td><tt class="literal">format_top_name</tt></td></tr><tr><td><tt class="literal">$:</tt></td><td><tt class="literal">format_line_break_characters</tt></td></tr><tr><td><tt class="literal">$^L</tt></td><td><tt class="literal">format_formfeed</tt></td></tr></table><p>Instead of saying:<blockquote><pre class="programlisting">$ofh = select(<em class="replaceable">HANDLE</em>);$~ = 'SomeFormat';$| = 1;select($ofh);</pre></blockquote>you can just say:<blockquote><pre class="programlisting">use FileHandle;<em class="replaceable">HANDLE</em>-&gt;format_name('SomeFormat');<em class="replaceable">HANDLE</em>-&gt;autoflush(1);</pre></blockquote>Currently, three methods (<tt class="literal">output_field_separator</tt>,<tt class="literal">output_record_separator</tt>, and <tt class="literal">input_record_separator</tt>) onlypretend to be per-handle methods: setting them on one handle actuallyaffects all filehandles. They are therefore only supported as classmethods, not as per-filehandle methods.  This restriction may belifted someday.</p><p>To get a lexically scoped filehandle, instead of usingfilehandle autovivification:<blockquote><pre class="programlisting">open my $fh, "&lt; somefile"    or die "can't open somefile: $!";</pre></blockquote>one could say:<blockquote><pre class="programlisting">use FileHandle;my $fh = FileHandle-&gt;new("&lt; somefile")    or die "can't open somefile: $!";</pre></blockquote><tt class="literal">FileHandle</tt> inherits from <tt class="literal">IO::File</tt>, which inherits from <tt class="literal">IO::Handle</tt> and<tt class="literal">IO::Seekable</tt>.  Virtually all the module's functionality is availablemore efficiently through basic, unadorned Perl calls, except for thefollowing, not all of which may be implemented on all non-Unixplatforms:</p><dl><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;blocking(</tt><em class="replaceable">EXPR</em><tt class="literal">)</tt></b></dt><dd><p>Called with an argument, enables nonblocking I/O if the argument isfalse, and disables nonblocking (that is, enables blocking) if theargument is true.  The method returns the previously set value (whichis still the current setting if no argument was given).  On error,<tt class="literal">blocking</tt> sets <tt class="literal">$!</tt> and returns <tt class="literal">undef</tt>.  This could be done using<tt class="literal">fcntl</tt> directly, but the <tt class="literal">FileHandle</tt> interface is much easier touse.</p></dd><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;clearerr</tt></b></dt><dd><p>Calls the C library function <em class="emphasis">clearerr</em>(3) to clear the handle'sinternal end-of-file and error status indicators.</p></dd><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;error</tt></b></dt><dd><p>Calls the C library function <em class="emphasis">ferror</em>(3) to test the error indicatorfor the given handle, returning whether that internal indicator isset.  The error indicator can be reset reliably only via the <tt class="literal">clearerr</tt>method.  (Some systems also reset it on calls to the <tt class="literal">seek</tt> operator.)</p></dd><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;formline(</tt><em class="replaceable">PICTURE</em><tt class="literal">,</tt> <em class="replaceable">LIST</em><tt class="literal">)</tt></b></dt><dd><p>This is the same as saving the old accumulator variable(<tt class="literal">$^A</tt>), calling the <tt class="literal">formline</tt>function with the given <em class="replaceable">PICTURE</em> and<em class="replaceable">LIST</em>, outputting the resulting contents ofthe accumulator to the given handle, and finally restoring theoriginal accumulator.  For example, here's how to output a long textvariable, with automatic word-wrapping at column 72:<blockquote><pre class="programlisting">use FileHandle;STDOUT-&gt;formline("^" . ("&lt;" x 72) . "~~\n", $long_text);</pre></blockquote></p></dd><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;getpos</tt></b></dt><dd><p>Calls the C library function <em class="emphasis">fgetpos</em>(3), providing an alternativeinterface to <tt class="literal">tell</tt>.  On some (non-UNIX) systems the return valuemay be a complex object, and <tt class="literal">getpos</tt> and <tt class="literal">setpos</tt> may be theonly way to portably reposition a text stream.</p></dd><dt><b><tt class="literal">FileHandle-&gt;new_tmpfile</tt></b></dt><dd><p>Calls the C library function <em class="emphasis">tmpfile</em>(3) to create a new temporaryfile opened for read-write mode and returns a handle to this stream.On systems where this is possible, the temporary file isanonymous--that is, it is <tt class="literal">unlink</tt>ed after creation, but held open.You should use this function, or <tt class="literal">POSIX::tmpnam</tt> as described under the <tt class="literal">POSIX</tt> module, to safely create a temporary file without exposingyourself to subtle but serious security problems through raceconditions. As of the 5.6.1 release of Perl, the <tt class="literal">File::Temp</tt> module is now the preferred interface.</p></dd><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;setbuf(</tt><em class="replaceable">BUFFER</em><tt class="literal">)</tt></b></dt><dd><p>Calls the C library function <em class="emphasis">setbuf</em>(3) with the given <em class="replaceable">BUFFER</em>variable.  It passes <tt class="literal">undef</tt> to indicate unbuffered output.  A variableused as a buffer by <tt class="literal">setbuf</tt> or <tt class="literal">setvbuf</tt><em class="emphasis">must not be modifiedin any way</em> until the handle is closed, or until <tt class="literal">setbuf</tt> or<tt class="literal">setvbuf</tt> is called again.  Otherwise, memory corruption may result,and you will be sad.</p></dd><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;setpos(</tt><em class="replaceable">EXPR</em><tt class="literal">)</tt></b></dt><dd><p>Calls the C library function <em class="emphasis">fsetpos</em>(3), providing an alternativeinterface to <tt class="literal">seek</tt>.  The argument should only be the return valuefrom <tt class="literal">getpos</tt>, described earlier.</p></dd><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;setvbuf(</tt><em class="replaceable">BUFFER</em><tt class="literal">,</tt> <em class="replaceable">TYPE</em><tt class="literal">,</tt> <em class="replaceable">SIZE</em><tt class="literal">)</tt></b></dt><dd><p>Calls the C library function <em class="emphasis">setvbuf</em>(3) with the given <em class="replaceable">BUFFER</em>.The standard C library constants <tt class="literal">_IONBF</tt> (unbuffered), <tt class="literal">_IOLBF</tt>(line buffered), and <tt class="literal">_IOFBF</tt> (fully buffered) are available forthe <em class="replaceable">TYPE</em> field if explicitly imported.  See the warningunder <tt class="literal">setbuf</tt>.</p></dd><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;sync</tt></b></dt><dd><p>Calls the C library function <em class="emphasis">fsync</em>(3) to synchronize a file'sin-memory state with the physical medium. Note that <tt class="literal">sync</tt> operates not on the handle, but on the file descriptor, so any data held bybuffers will not be synchronized unless flushed first.</p></dd><dt><b><em class="replaceable">HANDLE</em><tt class="literal">-&gt;untaint</tt></b></dt><dd><p>Marks the filehandle or directory handle as providing untainted data.When running under taint mode (see <a href="ch23_01.htm">Chapter 23, "Security"</a>), data read in from external files isconsidered untrustworthy.  Do not invoke this method blindly: you'recircumventing Perl's best attempts to protect you from yourself.</p></dd></dl><p></p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch32_25.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch32_27.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.25. File::Temp</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">32.27. Getopt::Long</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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