sdbm_file.html

来自「perl教程」· HTML 代码 · 共 144 行

HTML
144
字号
<?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>SDBM_File - Tied access to sdbm files</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__',1);</script>
<h1><a>SDBM_File - Tied access to sdbm files</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>
	<li><a href="#diagnostics">DIAGNOSTICS</a></li>
	<ul>

		<li><a href="#sdbm_store_returned_1__errno_22__key_____at____"><code>sdbm store returned -1, errno 22, key &quot;...&quot; at ...</code></a></li>
	</ul>

	<li><a href="#bugs_and_warnings">BUGS AND WARNINGS</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>SDBM_File - Tied access to sdbm files</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
 <span class="keyword">use</span> <span class="variable">Fcntl</span><span class="operator">;</span>   <span class="comment"># For O_RDWR, O_CREAT, etc.</span>
 <span class="keyword">use</span> <span class="variable">SDBM_File</span><span class="operator">;</span>
</pre>
<pre>
 <span class="keyword">tie</span><span class="operator">(</span><span class="variable">%h</span><span class="operator">,</span> <span class="string">'SDBM_File'</span><span class="operator">,</span> <span class="string">'filename'</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> <span class="number">0666</span><span class="operator">)</span>
   <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Couldn't tie SDBM file 'filename': $!; aborting"</span><span class="operator">;</span>
</pre>
<pre>
 <span class="comment"># Now read and change the hash</span>
 <span class="variable">$h</span><span class="operator">{</span><span class="string">newkey</span><span class="operator">}</span> <span class="operator">=</span> <span class="variable">newvalue</span><span class="operator">;</span>
 <span class="keyword">print</span> <span class="variable">$h</span><span class="operator">{</span><span class="string">oldkey</span><span class="operator">}</span><span class="operator">;</span> 
 <span class="operator">...</span>
</pre>
<pre>
 <span class="keyword">untie</span> <span class="variable">%h</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p><code>SDBM_File</code> establishes a connection between a Perl hash variable and
a file in SDBM_File format;.  You can manipulate the data in the file
just as if it were in a Perl hash, but when your program exits, the
data will remain in the file, to be used the next time your program
runs.</p>
<p>Use <code>SDBM_File</code> with the Perl built-in <a href="../lib/Pod/perlfunc.html#item_tie"><code>tie</code></a> function to establish
the connection between the variable and the file.  The arguments to
<a href="../lib/Pod/perlfunc.html#item_tie"><code>tie</code></a> should be:</p>
<ol>
<li>
<p>The hash variable you want to tie.</p>
</li>
<li>
<p>The string <code>&quot;SDBM_File&quot;</code>.  (Ths tells Perl to use the <code>SDBM_File</code>
package to perform the functions of the hash.)</p>
</li>
<li>
<p>The name of the file you want to tie to the hash.</p>
</li>
<li>
<p>Flags.  Use one of:</p>
<dl>
<dt><strong><a name="item_o_rdonly"><code>O_RDONLY</code></a></strong>

<dd>
<p>Read-only access to the data in the file.</p>
</dd>
</li>
<dt><strong><a name="item_o_wronly"><code>O_WRONLY</code></a></strong>

<dd>
<p>Write-only access to the data in the file.</p>
</dd>
</li>
<dt><strong><a name="item_o_rdwr"><code>O_RDWR</code></a></strong>

<dd>
<p>Both read and write access.</p>
</dd>
</li>
</dl>
<p>If you want to create the file if it does not exist, add <code>O_CREAT</code> to
any of these, as in the example.  If you omit <code>O_CREAT</code> and the file
does not already exist, the <a href="../lib/Pod/perlfunc.html#item_tie"><code>tie</code></a> call will fail.</p>
<li>
<p>The default permissions to use if a new file is created.  The actual
permissions will be modified by the user's umask, so you should
probably use 0666 here. (See <a href="../lib/Pod/perlfunc.html#umask">umask in the perlfunc manpage</a>.)</p>
</li>
</ol>
<p>
</p>
<hr />
<h1><a name="diagnostics">DIAGNOSTICS</a></h1>
<p>On failure, the <a href="../lib/Pod/perlfunc.html#item_tie"><code>tie</code></a> call returns an undefined value and probably
sets <a href="../lib/Pod/perlvar.html#item___"><code>$!</code></a> to contain the reason the file could not be tied.</p>
<p>
</p>
<h2><a name="sdbm_store_returned_1__errno_22__key_____at____"><code>sdbm store returned -1, errno 22, key &quot;...&quot; at ...</code></a></h2>
<p>This warning is emitted when you try to store a key or a value that
is too long.  It means that the change was not recorded in the
database.  See BUGS AND WARNINGS below.</p>
<p>
</p>
<hr />
<h1><a name="bugs_and_warnings">BUGS AND WARNINGS</a></h1>
<p>There are a number of limits on the size of the data that you can
store in the SDBM file.  The most important is that the length of a
key, plus the length of its associated value, may not exceed 1008
bytes.</p>
<p>See <a href="../lib/Pod/perlfunc.html#tie">tie in the perlfunc manpage</a>, <a href="../lib/Pod/perldbmfilter.html">the perldbmfilter manpage</a>, <a href="../lib/Fcntl.html">the Fcntl manpage</a></p>

</body>

</html>

⌨️ 快捷键说明

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