📄 ch08_189.htm
字号:
<html><head><title>Storable (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_188.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_190.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.189. Storable</h2><p><em class="filename">Storable.pm</em><a name="INDEX-1553" /> allows you to keep peristent state onyour Perl data structures, which include objects of type<tt class="literal">SCALAR</tt>, <tt class="literal">ARRAY</tt>,<tt class="literal">HASH</tt>, and <tt class="literal">REF</tt>. In other words,Storable lets you deal with any Perl types that can be convenientlystored on disk and retrieved at a later time. At the heart ofStorable is <tt class="literal">store</tt>, which takes a reference to theobject that will be stored and the filename where the informationshould be written. And, while you're statefullykeeping your Perl data on a filesystem, <tt class="literal">retrieve</tt>will open the file where the data is kept so you can work with it. Asof Perl 5.8, Storable is shipped with the source kit.</p><p>For the cost of a slight header overhead, you can store to an alreadyopened file descriptor using the <tt class="literal">store_fd</tt> routineand retrieve from a file via <tt class="literal">fd_retrieve</tt>. Thesenames aren't imported by default, so you will haveto do this explicitly if you need these routines. The file descriptoryou supply must be opened for read if you're goingto retrieve and for write if you wish to store. For example:</p><blockquote><pre class="code">#!/usr/local/bin/perl -wuse Storable;my $state_file = '/tmp/emp.cache';my %empdata = (EmployeeNumber => 1, Person => 'Nathan Patwardhan'); store(\%empdata, $state_file);my $h_ref = retrieve($state_file);</pre></blockquote><p>Storable implements the following methods.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>lock_retrieve</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>lock_retrieve()</pre><p>Gets an exclusive lock at the file before writing.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>lock_store</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>lock_store() </pre><p>Same as <tt class="literal">store</tt>, except that it gets an exclusivelock at the file before writing.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>nlock_store</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>nlock_store()</pre><p>Same as <tt class="literal">nstore()</tt>, except that it gets an exclusivelock at the file before writing.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>nstore_fd</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>nstore_fd()</pre><p>Allows you to store data in network order so it can be shared acrossplatforms. It also stores stringified double values to ensureportability. <tt class="literal">nstore_fd</tt> works the same as<tt class="literal">store_fd</tt> and behaves the same if errors areencountered.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>retrieve</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>retrieve(<em class="replaceable">file</em>)</pre><p>Takes <em class="replaceable">file</em> as an option and recreates theobjects in memory for you; a reference to the object is returned.<tt class="literal">retrieve</tt> returns <tt class="literal">undef</tt> if anI/O error is encountered.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>store</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>store(<em class="replaceable">type</em>, <em class="replaceable">file</em>)</pre><p>Takes <em class="replaceable">type</em> (<tt class="literal">HASH</tt>,<tt class="literal">SCALAR</tt>, etc.) and writes it to<em class="replaceable"><tt>file</tt></em>. <tt class="literal">store</tt> returns<tt class="literal">undef</tt> if an I/O error is encountered.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>store_fd</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>store_fd(<em class="replaceable">type</em>, <em class="replaceable">fh</em>)</pre><p>Allows you to store <em class="replaceable">type</em> in an existing<em class="replaceable"><tt>fh</tt></em> (filehandle). Similarly, you canretrieve data from <em class="replaceable"><tt>fh</tt></em> using<tt class="literal">fd_retrieve</tt>. If <em class="replaceable"><tt>fh</tt></em>isn't open, both <tt class="literal">store_fd</tt> and<tt class="literal">fd_retrieve</tt> will fail.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_188.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_190.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.188. sort</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.190. strict</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><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 + -