home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _93cc52612f0fb2fb3bb12b99771c6442 < prev    next >
Text File  |  2000-03-23  |  14KB  |  309 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>MLDBM - store multi-level hash structure in single level tied hash</TITLE>
  5. <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> MLDBM - store multi-level hash structure in single level tied hash</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  24.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  25.     <UL>
  26.  
  27.         <LI><A HREF="#changing the defaults">Changing the Defaults</A></LI>
  28.         <LI><A HREF="#controlling serializer properties">Controlling Serializer Properties</A></LI>
  29.     </UL>
  30.  
  31.     <LI><A HREF="#examples">EXAMPLES</A></LI>
  32.     <LI><A HREF="#bugs">BUGS</A></LI>
  33.     <LI><A HREF="#warnings">WARNINGS</A></LI>
  34.     <LI><A HREF="#authors">AUTHORS</A></LI>
  35.     <LI><A HREF="#version">VERSION</A></LI>
  36.     <LI><A HREF="#see also">SEE ALSO</A></LI>
  37. </UL>
  38. <!-- INDEX END -->
  39.  
  40. <HR>
  41. <P>
  42. <H1><A NAME="name">NAME</A></H1>
  43. <P>MLDBM - store multi-level hash structure in single level tied hash</P>
  44. <P>
  45. <HR>
  46. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  47. <UL>
  48. <LI>Linux</LI>
  49. <LI>Solaris</LI>
  50. <LI>Windows</LI>
  51. </UL>
  52. <HR>
  53. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  54. <PRE>
  55.     use MLDBM;                          # this gets the default, SDBM
  56.     #use MLDBM qw(DB_File FreezeThaw);  # use FreezeThaw for serializing
  57.     #use MLDBM qw(DB_File Storable);    # use Storable for serializing
  58. </PRE>
  59. <PRE>
  60.  
  61.     $dbm = tie %o, 'MLDBM' [..other DBM args..] or die $!;</PRE>
  62. <P>
  63. <HR>
  64. <H1><A NAME="description">DESCRIPTION</A></H1>
  65. <P>This module can serve as a transparent interface to any TIEHASH package
  66. that is required to store arbitrary perl data, including nested references.
  67. Thus, this module can be used for storing references and other arbitrary data
  68. within DBM databases.</P>
  69. <P>It works by serializing the references in the hash into a single string. In the
  70. underlying TIEHASH package (usually a DBM database), it is this string that
  71. gets stored.  When the value is fetched again, the string is deserialized to
  72. reconstruct the data structure into memory.</P>
  73. <P>For historical and practical reasons, it requires the <STRONG>Data::Dumper</STRONG> package,
  74. available at any CPAN site. <STRONG>Data::Dumper</STRONG> gives you really nice-looking dumps of
  75. your data structures, in case you wish to look at them on the screen, and
  76. it was the only serializing engine before version 2.00.  However, as of version
  77. 2.00, you can use any of <STRONG>Data::Dumper</STRONG>, <STRONG>FreezeThaw</STRONG> or <STRONG>Storable</STRONG> to
  78. perform the underlying serialization, as hinted at by the <EM>SYNOPSIS</EM> overview
  79. above.  Using <STRONG>Storable</STRONG> is usually much faster than the other methods.</P>
  80. <P>See the <EM>BUGS</EM> section for important limitations.</P>
  81. <P>
  82. <H2><A NAME="changing the defaults">Changing the Defaults</A></H2>
  83. <P><STRONG>MLDBM</STRONG> relies on an underlying TIEHASH implementation (usually a
  84. DBM package), and an underlying serialization package.  The respective
  85. defaults are <STRONG>SDBM_File</STRONG> and D<Data::Dumper>.  Both of these defaults
  86. can be changed.  Changing the <STRONG>SDBM_File</STRONG> default is strongly recommended.
  87. See <EM>WARNINGS</EM> below.</P>
  88. <P>Three serialization wrappers are currently supported: <STRONG>Data::Dumper</STRONG>,
  89. <STRONG>Storable</STRONG>, and <STRONG>FreezeThaw</STRONG>.  Additional serializers can be
  90. supported by writing a wrapper that implements the interface required by
  91. <STRONG>MLDBM::Serializer</STRONG>.  See the supported wrappers and the <STRONG>MLDBM::Serializer</STRONG>
  92. source for details.</P>
  93. <P>In the following, <EM>$OBJ</EM> stands for the tied object, as in:</P>
  94. <PRE>
  95.         $obj = tie %o, ....
  96.         $obj = tied %o;</PRE>
  97. <DL>
  98. <DT><STRONG><A NAME="item_UseDB">$MLDBM::UseDB    <EM>or</EM>    <EM>$OBJ</EM>->UseDB(<EM>[TIEDOBJECT]</EM>)</A></STRONG><BR>
  99. <DD>
  100. The global <CODE>$MLDBM::UseDB</CODE> can be set to default to something other than
  101. <CODE>SDBM_File</CODE>, in case you have a more efficient DBM, or if you want to use
  102. this with some other TIEHASH implementation.  Alternatively, you can specify
  103. the name of the package at <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A> time, as the first ``parameter''.
  104. Nested module names can be specified as ``Foo::Bar''.
  105. <P>The corresponding method call returns the underlying TIEHASH object when
  106. called without arguments.  It can be called with any object that
  107. implements Perl's TIEHASH interface, to set that value.</P>
  108. <P></P>
  109. <DT><STRONG><A NAME="item_Serializer">$MLDBM::Serializer    <EM>or</EM>    <EM>$OBJ</EM>->Serializer(<EM>[SZROBJECT]</EM>)</A></STRONG><BR>
  110. <DD>
  111. The global <CODE>$MLDBM::Serializer</CODE> can be set to the name of the serializing
  112. package to be used. Currently can be set to one of <CODE>Data::Dumper</CODE>,
  113. <CODE>Storable</CODE>, or <CODE>FreezeThaw</CODE>. Defaults to <CODE>Data::Dumper</CODE>.  Alternatively,
  114. you can specify the name of the serializer package at <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A> time, as the
  115. second ``parameter''.
  116. <P>The corresponding method call returns the underlying MLDBM serializer object
  117. when called without arguments.  It can be called with an object that
  118. implements the MLDBM serializer interface, to set that value.</P>
  119. <P></P></DL>
  120. <P>
  121. <H2><A NAME="controlling serializer properties">Controlling Serializer Properties</A></H2>
  122. <P>These methods are meant to supply an interface to the properties of the
  123. underlying serializer used.  Do <STRONG>not</STRONG> call or set them without
  124. understanding the consequences in full.  The defaults are usually sensible.</P>
  125. <P>Not all of these necessarily apply to all the supplied serializers, so we
  126. specify when to apply them.  Failure to respect this will usually lead to
  127. an exception.</P>
  128. <DL>
  129. <DT><STRONG><A NAME="item_DumpMeth">$MLDBM::DumpMeth    <EM>or</EM>  <EM>$OBJ</EM>->DumpMeth(<EM>[METHNAME]</EM>)</A></STRONG><BR>
  130. <DD>
  131. If the serializer provides alternative serialization methods, this
  132. can be used to set them.
  133. <P>With <STRONG>Data::Dumper</STRONG> (which offers a pure Perl and an XS verion
  134. of its serializing routine), this is set to <CODE>Dumpxs</CODE> by default if that
  135. is supported in your installation.  Otherwise, defaults to the slower
  136. <CODE>Dump</CODE> method.</P>
  137. <P>With <STRONG>Storable</STRONG>, a value of <CODE>portable</CODE> requests that serialization be
  138. architecture neutral, i.e. the deserialization can later occur on another
  139. platform. Of course, this only makes sense if your database files are
  140. themselves architecture neutral.  By default, native format is used for
  141. greater serializing speed in <STRONG>Storable</STRONG>.  Both <STRONG>Data::Dumper</STRONG> and
  142. <STRONG>FreezeThaw</STRONG> are always architecture neutral.</P>
  143. <P><STRONG>FreezeThaw</STRONG> does not honor this attribute.</P>
  144. <P></P>
  145. <DT><STRONG><A NAME="item_Key">$MLDBM::Key  <EM>or</EM>  <EM>$OBJ</EM>->Key(<EM>[KEYSTRING]</EM>)</A></STRONG><BR>
  146. <DD>
  147. If the serializer only deals with part of the data (perhaps because
  148. the TIEHASH object can natively store some types of data), it may need
  149. a unique key string to recognize the data it handles.  This can be used
  150. to set that string.  Best left alone.
  151. <P>Defaults to the magic string used to recognize MLDBM data. It is a six
  152. character wide, unique string. This is best left alone, unless you know
  153. what you are doing.</P>
  154. <P><STRONG>Storable</STRONG> and <STRONG>FreezeThaw</STRONG> do not honor this attribute.</P>
  155. <P></P>
  156. <DT><STRONG><A NAME="item_RemoveTaint">$MLDBM::RemoveTaint  <EM>or</EM>  <EM>$OBJ</EM>->RemoveTaint(<EM>[BOOL]</EM>)</A></STRONG><BR>
  157. <DD>
  158. If the serializer can optionally untaint any retrieved data subject to
  159. taint checks in Perl, this can be used to request that feature.  Data
  160. that comes from external sources (like disk-files) must always be
  161. viewed with caution, so use this only when you are sure that that is
  162. not an issue.
  163. <P><STRONG>Data::Dumper</STRONG> uses <A HREF="../../lib/Pod/perlfunc.html#item_eval"><CODE>eval()</CODE></A> to deserialize and is therefore subject to
  164. taint checks.  Can be set to a true value to make the <STRONG>Data::Dumper</STRONG>
  165. serializer untaint the data retrieved. It is not enabled by default.
  166. Use with care.</P>
  167. <P><STRONG>Storable</STRONG> and <STRONG>FreezeThaw</STRONG> do not honor this attribute.</P>
  168. <P></P></DL>
  169. <P>
  170. <HR>
  171. <H1><A NAME="examples">EXAMPLES</A></H1>
  172. <P>Here is a simple example.  Note that does not depend upon the underlying
  173. serializing package--most real life examples should not, usually.</P>
  174. <PRE>
  175.     use MLDBM;                          # this gets SDBM and Data::Dumper
  176.     #use MLDBM qw(SDBM_File Storable);  # SDBM and Storable
  177.     use Fcntl;                          # to get 'em constants
  178. </PRE>
  179. <PRE>
  180.  
  181.     $dbm = tie %o, 'MLDBM', 'testmldbm', O_CREAT|O_RDWR, 0640 or die $!;</PRE>
  182. <PRE>
  183.  
  184.     $c = [\ 'c'];
  185.     $b = {};
  186.     $a = [1, $b, $c];
  187.     $b->{a} = $a;
  188.     $b->{b} = $a->[1];
  189.     $b->{c} = $a->[2];
  190.     @o{qw(a b c)} = ($a, $b, $c);</PRE>
  191. <PRE>
  192.  
  193.     #
  194.     # to see what was stored
  195.     #
  196.     use Data::Dumper;
  197.     print Data::Dumper->Dump([@o{qw(a b c)}], [qw(a b c)]);</PRE>
  198. <PRE>
  199.  
  200.     #
  201.     # to modify data in a substructure
  202.     #
  203.     $tmp = $o{a};
  204.     $tmp->[0] = 'foo';
  205.     $o{a} = $tmp;</PRE>
  206. <PRE>
  207.  
  208.     #
  209.     # can access the underlying DBM methods transparently
  210.     #
  211.     #print $dbm->fd, "\n";              # DB_File method</PRE>
  212. <P>Here is another small example using Storable, in a portable format:</P>
  213. <PRE>
  214.     use MLDBM qw(DB_File Storable);     # DB_File and Storable
  215. </PRE>
  216. <PRE>
  217.  
  218.     tie %o, 'MLDBM', 'testmldbm', O_CREAT|O_RDWR, 0640 or die $!;</PRE>
  219. <PRE>
  220.  
  221.     (tied %o)->DumpMeth('portable');    # Ask for portable binary
  222.     $o{'ENV'} = \%ENV;                  # Stores the whole environment</PRE>
  223. <P>
  224. <HR>
  225. <H1><A NAME="bugs">BUGS</A></H1>
  226. <OL>
  227. <LI>
  228. Adding or altering substructures to a hash value is not entirely transparent
  229. in current perl.  If you want to store a reference or modify an existing
  230. reference value in the DBM, it must first be retrieved and stored in a
  231. temporary variable for further modifications.  In particular, something like
  232. this will NOT work properly:
  233. <PRE>
  234.         $mldb{key}{subkey}[3] = 'stuff';        # won't work</PRE>
  235. <P>Instead, that must be written as:</P>
  236. <PRE>
  237.         $tmp = $mldb{key};                      # retrieve value
  238.         $tmp->{subkey}[3] = 'stuff';
  239.         $mldb{key} = $tmp;                      # store value</PRE>
  240. <P>This limitation exists because the perl TIEHASH interface currently has no
  241. support for multidimensional ties.</P>
  242. <P></P>
  243. <LI>
  244. The <STRONG>Data::Dumper</STRONG> serializer uses eval().  A lot.  Try the <STRONG>Storable</STRONG>
  245. serializer, which is generally the most efficient.
  246. <P></P></OL>
  247. <P>
  248. <HR>
  249. <H1><A NAME="warnings">WARNINGS</A></H1>
  250. <OL>
  251. <LI>
  252. Many DBM implementations have arbitrary limits on the size of records
  253. that can be stored.  For example, SDBM and many ODBM or NDBM
  254. implementations have a default limit of 1024 bytes for the size of a
  255. record.  MLDBM can easily exceed these limits when storing large data
  256. structures, leading to mysterious failures.  Although SDBM_File is
  257. used by MLDBM by default, it is not a good choice if you're storing
  258. large data structures.  Berkeley DB and GDBM both do not have these
  259. limits, so I recommend using either of those instead.
  260. <P></P>
  261. <LI>
  262. MLDBM does well with data structures that are not too deep and not
  263. too wide.  You also need to be careful about how many <CODE>FETCH</CODE>es your
  264. code actually ends up doing.  Meaning, you should get the most mileage
  265. out of a <CODE>FETCH</CODE> by holding on to the highest level value for as long
  266. as you need it.  Remember that every toplevel access of the tied hash,
  267. for example <CODE>$mldb{foo}</CODE>, translates to a MLDBM <CODE>FETCH()</CODE> call.
  268. <P>Too often, people end up writing something like this:</P>
  269. <PRE>
  270.         tie %h, 'MLDBM', ...;
  271.         for my $k (keys %{$h{something}}) {
  272.             print $h{something}{$k}[0]{foo}{bar};  # FETCH _every_ time!
  273.         }</PRE>
  274. <P>when it should be written this for efficiency:</P>
  275. <PRE>
  276.         tie %h, 'MLDBM', ...;
  277.         my $root = $h{something};                  # FETCH _once_
  278.         for my $k (keys %$root) {
  279.             print $k->[0]{foo}{bar};
  280.         }</PRE>
  281. <P></P></OL>
  282. <P>
  283. <HR>
  284. <H1><A NAME="authors">AUTHORS</A></H1>
  285. <P>Gurusamy Sarathy <<EM><A HREF="mailto:gsar@umich.edu">gsar@umich.edu</A></EM>>.</P>
  286. <P>Support for multiple serializing packages by
  287. Raphael Manfredi <<EM><A HREF="mailto:Raphael_Manfredi@grenoble.hp.com">Raphael_Manfredi@grenoble.hp.com</A></EM>>.</P>
  288. <P>Copyright (c) 1995-98 Gurusamy Sarathy.  All rights reserved.</P>
  289. <P>Copyright (c) 1998 Raphael Manfredi.</P>
  290. <P>This program is free software; you can redistribute it and/or
  291. modify it under the same terms as Perl itself.</P>
  292. <P>
  293. <HR>
  294. <H1><A NAME="version">VERSION</A></H1>
  295. <P>Version 2.00    10 May 1998</P>
  296. <P>
  297. <HR>
  298. <H1><A NAME="see also">SEE ALSO</A></H1>
  299. <P>perl(1), perltie(1), perlfunc(1), Data::Dumper(3), FreezeThaw(3), Storable(3).</P>
  300. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  301. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  302. <STRONG><P CLASS=block> MLDBM - store multi-level hash structure in single level tied hash</P></STRONG>
  303. </TD></TR>
  304. </TABLE>
  305.  
  306. </BODY>
  307.  
  308. </HTML>
  309.