ch14_09.htm
来自「By Tom Christiansen and Nathan Torkingto」· HTM 代码 · 共 364 行
HTM
364 行
<HTML><HEAD><TITLE>Recipe 14.8. Storing Complex Data in a DBM File (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:42:57Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch14_01.htm"TITLE="14. Database Access"><LINKREL="prev"HREF="ch14_08.htm"TITLE="14.7. Treating a Text File as a Database Array"><LINKREL="next"HREF="ch14_10.htm"TITLE="14.9. Persistent Data"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch14_08.htm"TITLE="14.7. Treating a Text File as a Database Array"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.7. Treating a Text File as a Database Array"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch14_01.htm"TITLE="14. Database Access"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch14_10.htm"TITLE="14.9. Persistent Data"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.9. Persistent Data"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch14-18811">14.8. Storing Complex Data in a DBM File</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1014">Problem<ACLASS="indexterm"NAME="ch14-idx-1000004991-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000004991-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000004991-2"></A></A></H3><PCLASS="para">You want values in a DBM file to be something other than scalars. For instance, you use a hash of hashes in your program and want to store them in a DBM file for other programs to access, or you want them to persist across process runs.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1020">Solution</A></H3><PCLASS="para">Use the CPAN module MLDBM to values more complex values than strings and numbers.</P><PRECLASS="programlisting">use MLDBM 'DB_File';tie(%HASH, 'MLDBM', [... other DBM arguments]) or die $!;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1030">Discussion</A></H3><PCLASS="para">MLDBM uses Data::Dumper (see <ACLASS="xref"HREF="ch11_15.htm"TITLE="Transparently Persistent Data Structures">Recipe 11.14</A>) to convert data structures to and from strings so that they can be stored in a DBM file. It doesn't store references, instead it stores the data that the references refer to:</P><PRECLASS="programlisting"># %hash is a tied hash$hash{"Tom Christiansen"} = [ "book author", 'tchrist@perl.com' ]; $hash{"Tom Boutell"} = [ "shareware author", 'boutell@boutell.com' ];# names to compare$name1 = "Tom Christiansen";$name2 = "Tom Boutell";$tom1 = $hash{$name1}; # snag local pointer$tom2 = $hash{$name2}; # and another print "Two Toming: $tom1 $tom2\n";<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Tom Toming: ARRAY(0x73048) ARRAY(0x73e4c)</I></CODE></B></CODE></PRE><PCLASS="para">Each time MLDBM retrieves a data structure from the DBM file, it generates a new copy of that data. To compare data that you retrieve from a MLDBM database, you need to compare the values within the structure:</P><PRECLASS="programlisting">if ($tom1->[0] eq $tom2->[0] && $tom1->[1] eq $tom2->[1]) { print "You're having runtime fun with one Tom made two.\n";} else { print "No two Toms are ever alike.\n";}</PRE><PCLASS="para">This is more efficient than:</P><PRECLASS="programlisting">if ($hash{$name1}->[0] eq $hash{$name2}->[0] && # INEFFICIENT $hash{$name1}->[1] eq $hash{$name2}->[1]) { print "You're having runtime fun with one Tom made two.\n";} else { print "No two Toms are ever alike.\n";}</PRE><PCLASS="para">Each time we say <CODECLASS="literal">$hash{...}</CODE>, the DBM file is consulted. The inefficient code above accesses the database four times, whereas the code using the temporary variables <CODECLASS="literal">$tom1</CODE> and <CODECLASS="literal">$tom2</CODE> only accesses the database twice.</P><PCLASS="para">Current limitations of Perl's <CODECLASS="literal">tie</CODE> mechanism prevent you from storing or modifying parts of a MLDBM value directly:</P><PRECLASS="programlisting">$hash{"Tom Boutell"}->[0] = "Poet Programmer"; # WRONG</PRE><PCLASS="para">Always get, change, and set pieces of the stored structure through a temporary variable:</P><PRECLASS="programlisting">$entry = $hash{"Tom Boutell"}; # RIGHT$entry->[0] = "Poet Programmer";$hash{"Tom Boutell"} = $entry;</PRE><PCLASS="para">If MLDBM uses a database with size limits on values, like SDBM, you'll quickly hit those limits. To get around this, use GDBM_File or DB_File, which don't limit the size of keys or values. DB_File is the better choice because it is byte-order neutral, which lets the database be shared between both big- and little-endian architectures.<ACLASS="indexterm"NAME="ch14-idx-1000004996-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000004996-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000004996-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1108">See Also</A></H3><PCLASS="para">The documentation for the Data::Dumper, MLDBM, and Storable modules from CPAN; <ACLASS="xref"HREF="ch11_14.htm"TITLE="Storing Data Structures to Disk">Recipe 11.13</A>; <ACLASS="xref"HREF="ch14_10.htm"TITLE="Persistent Data">Recipe 14.9</A></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="ch14_08.htm"TITLE="14.7. Treating a Text File as a Database Array"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.7. Treating a Text File as a Database Array"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="ch14_10.htm"TITLE="14.9. Persistent Data"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.9. Persistent Data"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">14.7. Treating a Text File as a Database Array</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">14.9. Persistent Data</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 © 2002</a> O'Reilly & 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 + =
减小字号Ctrl + -
显示快捷键?