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

📄 ch32_11.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>DB_File (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="DB_File"><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_10.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_12.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.11. DB_File</h2><blockquote><pre class="programlisting">use DB_File;</pre></blockquote><p>Tie a hash to a DBM-style file:<blockquote><pre class="programlisting">tie(%hash, "DB_File", $filename)     # Open database.    or die "Can't open $filename: $!";$v = $hash{"key"};                   # Retrieve from database.$hash{"key"} = "value";              # Put value into database.untie %hash;</pre></blockquote>Tie a hash to a B-tree file, but still access as a regular DBM hash:<blockquote><pre class="programlisting">tie(%hash, "DB_File", "mytree", O_RDWR|O_CREAT, 0666, $DB_BTREE)    or die "Cannot open file `mytree': $!";while (($k, $v) = each %hash) {     # Do in-order traversal.    print "$k =&gt; $v\n";}</pre></blockquote>Tie an array to a plain text file:<blockquote><pre class="programlisting">tie(@lines, "DB_File", $textfile, O_RDWR|O_CREAT, 0666, $DB_RECNO)    or die "Cannot open textfile $textfile: $!";# Write a few lines to the file, overwriting any old contents.$lines[0] = "first line";$lines[1] = "second line";$lines[2] = "third line";push @lines, "penult", "last";  # Append two lines to the file.$wc = scalar @lines;            # Count lines in file.$last = pop @lines;             # Delete and retrieve last line.</pre></blockquote>The <tt class="literal">DB_File</tt> module provides tied access to BerkeleyDB.<a href="#FOOTNOTE-1">[1]</a> The default <tt class="literal">tie</tt> functiongives you a standard DBM-style database with some features that noother DBM library provides: there are no size limits on either keys orvalues, and your data is stored in a byte-order independent format.</p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1]Providing you have that library installed on yoursystem. If not, you can build and install it easilyenough.</p></blockquote><p>The second <tt class="literal">tie</tt> mechanism uses B-trees to give you atrue ISAM (indexed sequential access method) file, that is, a hashwhose keys are automatically ordered--alphabetically by default,but configurable by the user.</p><p>The third <tt class="literal">tie</tt> mechanism binds an array to a file ofrecords (text lines by default) so that changes to the array areautomatically reflected on disk.  This simulates random access by linenumber on a regular text file.  The standard interface conforms toversion 1.x of Berkeley DB; if you want to make use of the newfeatures available in Berkeley DB 2.x or 3.x, use the CPAN module<tt class="literal">BerkeleyDB</tt> instead.</p><p>Starting with version 2.x, Berkeley DB has internal support forlocking; earlier versions did not.  See the section "FileLocking" in <a href="ch16_01.htm">Chapter 16, "Interprocess Communication"</a> for adescription of how you can safely lock any kind of database file using<tt class="literal">flock</tt> on a semaphore file.</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_10.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_12.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.10. Data::Dumper</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.12. Dumpvalue</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 + -