📄 ch14_10.htm
字号:
<HTML><HEAD><TITLE>Recipe 14.9. Persistent Data (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:59Z"><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_09.htm"TITLE="14.8. Storing Complex Data in a DBM File"><LINKREL="next"HREF="ch14_11.htm"TITLE="14.10. Executing an SQL Command Using DBI and DBD"></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_09.htm"TITLE="14.8. Storing Complex Data in a DBM File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.8. Storing Complex Data in a DBM File"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_11.htm"TITLE="14.10. Executing an SQL Command Using DBI and DBD"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.10. Executing an SQL Command Using DBI and DBD"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch14-37320">14.9. Persistent Data</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1118">Problem<ACLASS="indexterm"NAME="ch14-idx-1000005000-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000005000-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000005000-2"></A></A></H3><PCLASS="para">You want your variables to retain their values between calls to your program.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1124">Solution</A></H3><PCLASS="para">Use a MLDBM to store the values between calls to your program:</P><PRECLASS="programlisting">use MLDBM 'DB_File';my ($VARIABLE1,$VARIABLE2);my $Persistent_Store = '/projects/foo/data';BEGIN { my %data; tie(%data, 'MLDBM', $Persistent_Store) or die "Can't tie to $Persistent_Store : $!"; $VARIABLE1 = $data{VARIABLE1}; $VARIABLE2 = $data{VARIABLE2}; # ... untie %data;}END { my %data; tie (%data, 'MLDBM', $Persistent_Store) or die "Can't tie to $Persistent_Store : $!"; $data{VARIABLE1} = $VARIABLE1; $data{VARIABLE2} = $VARIABLE2; # ... untie %data;}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1174">Discussion</A></H3><PCLASS="para">An important limitation of MLDBM is that you can't add to or alter the structure in the reference without assignment to a temporary variable. We do this in the sample program in <ACLASS="xref"HREF="ch14_10.htm#ch14-12839"TITLE="mldbm-demo">Example 14.6</A>, assigning to <CODECLASS="literal">$array_ref</CODE> before we <CODECLASS="literal">push</CODE>. You simply can't do this:</P><PRECLASS="programlisting">push(@{$db{$user}}, $duration);</PRE><PCLASS="para">For a start, MLDBM doesn't allow it. Also, <CODECLASS="literal">$db{$user}</CODE> might not be in the database (the array reference isn't automatically created as it would be if <CODECLASS="literal">%db</CODE> weren't tied to a DBM file). This is why we test <CODECLASS="literal">exists</CODE> <CODECLASS="literal">$db{$user}</CODE> when we give <CODECLASS="literal">$array_ref</CODE> its initial value. We're creating the empty array for the case where it doesn't already exist.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch14-12839">Example 14.6: mldbm-demo</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch14-idx-1000005001-0"></A>mldbm_demo - show how to use MLDBM with DB_Fileuse MLDBM "DB_File";$db = "/tmp/mldbm-array";tie %db, 'MLDBM', $db or die "Can't open $db : $!";while(<DATA>) { chomp; ($user, $duration) = split(/\s+/, $_); $array_ref = exists $db{$user} ? $db{$user} : []; push(@$array_ref, $duration); $db{$user} = $array_ref;}foreach $user (sort keys %db) { print "$user: "; $total = 0; foreach $duration (@{ $db{$user} }) { print "$duration "; $total += $duration; } print "($total)\n"; }__END__gnat 15.3tchrist 2.5jules 22.1tchrist 15.9gnat 8.7<ACLASS="indexterm"NAME="ch14-idx-1000005003-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000005003-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000005003-2"></A></PRE></DIV><PCLASS="para">Newer versions of MLDBM allow you to select not just the database module (we recommend DB_File), but also the serialization module (we recommend Storable). Previous versions limited you to Data::Dumper for serializing, which is slower than Storable. Here's how you use DB_File with Storable:</P><PRECLASS="programlisting">use MLDBM qw(DB_File Storable);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1252">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_09.htm"TITLE="Storing Complex Data in a DBM File">Recipe 14.8</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_09.htm"TITLE="14.8. Storing Complex Data in a DBM File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.8. Storing Complex Data in a DBM File"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_11.htm"TITLE="14.10. Executing an SQL Command Using DBI and DBD"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.10. Executing an SQL Command Using DBI and DBD"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">14.8. Storing Complex Data in a DBM File</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.10. Executing an SQL Command Using DBI and DBD</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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -