file.html
来自「perl教程」· HTML 代码 · 共 657 行 · 第 1/3 页
HTML
657 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Tie::File - Access the lines of a disk file via a Perl array</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>Tie::File - Access the lines of a disk file via a Perl array</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#recsep"><code>recsep</code></a></li>
<li><a href="#autochomp"><code>autochomp</code></a></li>
<li><a href="#mode"><code>mode</code></a></li>
<li><a href="#memory"><code>memory</code></a></li>
<li><a href="#dw_size"><code>dw_size</code></a></li>
<li><a href="#option_format">Option Format</a></li>
</ul>
<li><a href="#public_methods">Public Methods</a></li>
<ul>
<li><a href="#flock"><a href="../../lib/Pod/perlfunc.html#item_flock"><code>flock</code></a></a></li>
<li><a href="#autochomp"><code>autochomp</code></a></li>
<li><a href="#defer__flush__discard__and_autodefer"><code>defer</code>, <code>flush</code>, <code>discard</code>, and <code>autodefer</code></a></li>
<li><a href="#offset"><code>offset</code></a></li>
</ul>
<li><a href="#tying_to_an_alreadyopened_filehandle">Tying to an already-opened filehandle</a></li>
<li><a href="#deferred_writing">Deferred Writing</a></li>
<ul>
<li><a href="#autodeferring">Autodeferring</a></li>
</ul>
<li><a href="#concurrent_access_to_files">CONCURRENT ACCESS TO FILES</a></li>
<li><a href="#caveats">CAVEATS</a></li>
<li><a href="#subclassing">SUBCLASSING</a></li>
<li><a href="#what_about_db_file">WHAT ABOUT <code>DB_File</code>?</a></li>
<li><a href="#author">AUTHOR</a></li>
<li><a href="#license">LICENSE</a></li>
<li><a href="#warranty">WARRANTY</a></li>
<li><a href="#thanks">THANKS</a></li>
<li><a href="#todo">TODO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Tie::File - Access the lines of a disk file via a Perl array</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="comment"># This file documents Tie::File version 0.97</span>
<span class="keyword">use</span> <span class="variable">Tie::File</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">tie</span> <span class="variable">@array</span><span class="operator">,</span> <span class="string">'Tie::File'</span><span class="operator">,</span> <span class="variable">filename</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="operator">...;</span>
</pre>
<pre>
<span class="variable">$array</span><span class="operator">[</span><span class="number">13</span><span class="operator">]</span> <span class="operator">=</span> <span class="string">'blah'</span><span class="operator">;</span> <span class="comment"># line 13 of the file is now 'blah'</span>
<span class="keyword">print</span> <span class="variable">$array</span><span class="operator">[</span><span class="number">42</span><span class="operator">]</span><span class="operator">;</span> <span class="comment"># display line 42 of the file</span>
</pre>
<pre>
<span class="variable">$n_recs</span> <span class="operator">=</span> <span class="variable">@array</span><span class="operator">;</span> <span class="comment"># how many records are in the file?</span>
<span class="variable">$#array</span> <span class="operator">-=</span> <span class="number">2</span><span class="operator">;</span> <span class="comment"># chop two records off the end</span>
</pre>
<pre>
<span class="keyword">for</span> <span class="operator">(</span><span class="variable">@array</span><span class="operator">)</span> <span class="operator">{</span>
<span class="regex">s/PERL/Perl/g</span><span class="operator">;</span> <span class="comment"># Replace PERL with Perl everywhere in the file</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="comment"># These are just like regular push, pop, unshift, shift, and splice</span>
<span class="comment"># Except that they modify the file in the way you would expect</span>
</pre>
<pre>
<span class="keyword">push</span> <span class="variable">@array</span><span class="operator">,</span> <span class="variable">new</span> <span class="variable">recs</span><span class="operator">...;</span>
<span class="keyword">my</span> <span class="variable">$r1</span> <span class="operator">=</span> <span class="keyword">pop</span> <span class="variable">@array</span><span class="operator">;</span>
<span class="keyword">unshift</span> <span class="variable">@array</span><span class="operator">,</span> <span class="variable">new</span> <span class="variable">recs</span><span class="operator">...;</span>
<span class="keyword">my</span> <span class="variable">$r2</span> <span class="operator">=</span> <span class="keyword">shift</span> <span class="variable">@array</span><span class="operator">;</span>
<span class="variable">@old_recs</span> <span class="operator">=</span> <span class="keyword">splice</span> <span class="variable">@array</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">7</span><span class="operator">,</span> <span class="variable">new</span> <span class="variable">recs</span><span class="operator">...;</span>
</pre>
<pre>
<span class="keyword">untie</span> <span class="variable">@array</span><span class="operator">;</span> <span class="comment"># all finished</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p><code>Tie::File</code> represents a regular text file as a Perl array. Each
element in the array corresponds to a record in the file. The first
line of the file is element 0 of the array; the second line is element
1, and so on.</p>
<p>The file is <em>not</em> loaded into memory, so this will work even for
gigantic files.</p>
<p>Changes to the array are reflected in the file immediately.</p>
<p>Lazy people and beginners may now stop reading the manual.</p>
<p>
</p>
<h2><a name="recsep"><code>recsep</code></a></h2>
<p>What is a 'record'? By default, the meaning is the same as for the
<code><...></code> operator: It's a string terminated by <a href="../../lib/Pod/perlvar.html#item___"><code>$/</code></a>, which is
probably <code>"\n"</code>. (Minor exception: on DOS and Win32 systems, a
'record' is a string terminated by <code>"\r\n"</code>.) You may change the
definition of "record" by supplying the <code>recsep</code> option in the <a href="../../lib/Pod/perlfunc.html#item_tie"><code>tie</code></a>
call:</p>
<pre>
<span class="keyword">tie</span> <span class="variable">@array</span><span class="operator">,</span> <span class="string">'Tie::File'</span><span class="operator">,</span> <span class="variable">$file</span><span class="operator">,</span> <span class="string">recsep</span> <span class="operator">=></span> <span class="string">'es'</span><span class="operator">;</span>
</pre>
<p>This says that records are delimited by the string <code>es</code>. If the file
contained the following data:</p>
<pre>
Curse these pesky flies!\n</pre>
<p>then the <code>@array</code> would appear to have four elements:</p>
<pre>
"Curse th"
"e p"
"ky fli"
"!\n"</pre>
<p>An undefined value is not permitted as a record separator. Perl's
special "paragraph mode" semantics (à la <a href="../../lib/Pod/perlvar.html#item___"><code>$/ = ""</code></a>) are not
emulated.</p>
<p>Records read from the tied array do not have the record separator
string on the end; this is to allow</p>
<pre>
<span class="variable">$array</span><span class="operator">[</span><span class="number">17</span><span class="operator">]</span> <span class="operator">.=</span> <span class="string">"extra"</span><span class="operator">;</span>
</pre>
<p>to work as expected.</p>
<p>(See <a href="#autochomp">autochomp</a>, below.) Records stored into the array will have
the record separator string appended before they are written to the
file, if they don't have one already. For example, if the record
separator string is <code>"\n"</code>, then the following two lines do exactly
the same thing:</p>
<pre>
<span class="variable">$array</span><span class="operator">[</span><span class="number">17</span><span class="operator">]</span> <span class="operator">=</span> <span class="string">"Cherry pie"</span><span class="operator">;</span>
<span class="variable">$array</span><span class="operator">[</span><span class="number">17</span><span class="operator">]</span> <span class="operator">=</span> <span class="string">"Cherry pie\n"</span><span class="operator">;</span>
</pre>
<p>The result is that the contents of line 17 of the file will be
replaced with "Cherry pie"; a newline character will separate line 17
from line 18. This means that this code will do nothing:</p>
<pre>
<span class="keyword">chomp</span> <span class="variable">$array</span><span class="operator">[</span><span class="number">17</span><span class="operator">]</span><span class="operator">;</span>
</pre>
<p>Because the <a href="../../lib/Pod/perlfunc.html#item_chomp"><code>chomp</code></a>ed value will have the separator reattached when
it is written back to the file. There is no way to create a file
whose trailing record separator string is missing.</p>
<p>Inserting records that <em>contain</em> the record separator string is not
supported by this module. It will probably produce a reasonable
result, but what this result will be may change in a future version.
Use 'splice' to insert records or to replace one record with several.</p>
<p>
</p>
<h2><a name="autochomp"><code>autochomp</code></a></h2>
<p>Normally, array elements have the record separator removed, so that if
the file contains the text</p>
<pre>
Gold
Frankincense
Myrrh</pre>
<p>the tied array will appear to contain <code>("Gold", "Frankincense",
"Myrrh")</code>. If you set <code>autochomp</code> to a false value, the record
separator will not be removed. If the file above was tied with</p>
<pre>
<span class="keyword">tie</span> <span class="variable">@gifts</span><span class="operator">,</span> <span class="string">"Tie::File"</span><span class="operator">,</span> <span class="variable">$gifts</span><span class="operator">,</span> <span class="string">autochomp</span> <span class="operator">=></span> <span class="number">0</span><span class="operator">;</span>
</pre>
<p>then the array <code>@gifts</code> would appear to contain <code>("Gold\n",
"Frankincense\n", "Myrrh\n")</code>, or (on Win32 systems) <code>("Gold\r\n",
"Frankincense\r\n", "Myrrh\r\n")</code>.</p>
<p>
</p>
<h2><a name="mode"><code>mode</code></a></h2>
<p>Normally, the specified file will be opened for read and write access,
and will be created if it does not exist. (That is, the flags
<code>O_RDWR | O_CREAT</code> are supplied in the <a href="../../lib/Pod/perlfunc.html#item_open"><code>open</code></a> call.) If you want to
change this, you may supply alternative flags in the <code>mode</code> option.
See <a href="../../lib/Fcntl.html">the Fcntl manpage</a> for a listing of available flags.
For example:</p>
<pre>
<span class="comment"># open the file if it exists, but fail if it does not exist</span>
<span class="keyword">use</span> <span class="variable">Fcntl</span> <span class="string">'O_RDWR'</span><span class="operator">;</span>
<span class="keyword">tie</span> <span class="variable">@array</span><span class="operator">,</span> <span class="string">'Tie::File'</span><span class="operator">,</span> <span class="variable">$file</span><span class="operator">,</span> <span class="string">mode</span> <span class="operator">=></span> <span class="variable">O_RDWR</span><span class="operator">;</span>
</pre>
<pre>
<span class="comment"># create the file if it does not exist</span>
<span class="keyword">use</span> <span class="variable">Fcntl</span> <span class="string">'O_RDWR'</span><span class="operator">,</span> <span class="string">'O_CREAT'</span><span class="operator">;</span>
<span class="keyword">tie</span> <span class="variable">@array</span><span class="operator">,</span> <span class="string">'Tie::File'</span><span class="operator">,</span> <span class="variable">$file</span><span class="operator">,</span> <span class="string">mode</span> <span class="operator">=></span> <span class="variable">O_RDWR</span> <span class="operator">|</span> <span class="variable">O_CREAT</span><span class="operator">;</span>
</pre>
<pre>
<span class="comment"># open an existing file in read-only mode</span>
<span class="keyword">use</span> <span class="variable">Fcntl</span> <span class="string">'O_RDONLY'</span><span class="operator">;</span>
<span class="keyword">tie</span> <span class="variable">@array</span><span class="operator">,</span> <span class="string">'Tie::File'</span><span class="operator">,</span> <span class="variable">$file</span><span class="operator">,</span> <span class="string">mode</span> <span class="operator">=></span> <span class="variable">O_RDONLY</span><span class="operator">;</span>
</pre>
<p>Opening the data file in write-only or append mode is not supported.</p>
<p>
</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?