home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / lib / Tie / Hash.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-19  |  7.0 KB  |  244 lines

  1. package Tie::Hash;
  2.  
  3. our $VERSION = '1.00';
  4.  
  5. =head1 NAME
  6.  
  7. Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.     package NewHash;
  12.     require Tie::Hash;
  13.  
  14.     @ISA = (Tie::Hash);
  15.  
  16.     sub DELETE { ... }        # Provides needed method
  17.     sub CLEAR { ... }        # Overrides inherited method
  18.  
  19.  
  20.     package NewStdHash;
  21.     require Tie::Hash;
  22.  
  23.     @ISA = (Tie::StdHash);
  24.  
  25.     # All methods provided by default, define only those needing overrides
  26.     # Accessors access the storage in %{$_[0]};
  27.     # TIEHANDLE should return a reference to the actual storage
  28.     sub DELETE { ... }
  29.  
  30.     package NewExtraHash;
  31.     require Tie::Hash;
  32.  
  33.     @ISA = (Tie::ExtraHash);
  34.  
  35.     # All methods provided by default, define only those needing overrides
  36.     # Accessors access the storage in %{$_[0][0]};
  37.     # TIEHANDLE should return an array reference with the first element being
  38.     # the reference to the actual storage 
  39.     sub DELETE { 
  40.       $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer
  41.       delete $_[0][0]->{$_[1]};          #  $_[0]->SUPER::DELETE($_[1]) }
  42.  
  43.  
  44.     package main;
  45.  
  46.     tie %new_hash, 'NewHash';
  47.     tie %new_std_hash, 'NewStdHash';
  48.     tie %new_extra_hash, 'NewExtraHash',
  49.     sub {warn "Doing \U$_[1]\E of $_[2].\n"};
  50.  
  51. =head1 DESCRIPTION
  52.  
  53. This module provides some skeletal methods for hash-tying classes. See
  54. L<perltie> for a list of the functions required in order to tie a hash
  55. to a package. The basic B<Tie::Hash> package provides a C<new> method, as well
  56. as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> and
  57. B<Tie::ExtraHash> packages
  58. provide most methods for hashes described in L<perltie> (the exceptions
  59. are C<UNTIE> and C<DESTROY>).  They cause tied hashes to behave exactly like standard hashes,
  60. and allow for selective overwriting of methods.  B<Tie::Hash> grandfathers the
  61. C<new> method: it is used if C<TIEHASH> is not defined
  62. in the case a class forgets to include a C<TIEHASH> method.
  63.  
  64. For developers wishing to write their own tied hashes, the required methods
  65. are briefly defined below. See the L<perltie> section for more detailed
  66. descriptive, as well as example code:
  67.  
  68. =over 4
  69.  
  70. =item TIEHASH classname, LIST
  71.  
  72. The method invoked by the command C<tie %hash, classname>. Associates a new
  73. hash instance with the specified class. C<LIST> would represent additional
  74. arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
  75. complete the association.
  76.  
  77. =item STORE this, key, value
  78.  
  79. Store datum I<value> into I<key> for the tied hash I<this>.
  80.  
  81. =item FETCH this, key
  82.  
  83. Retrieve the datum in I<key> for the tied hash I<this>.
  84.  
  85. =item FIRSTKEY this
  86.  
  87. Return the first key in the hash.
  88.  
  89. =item NEXTKEY this, lastkey
  90.  
  91. Return the next key in the hash.
  92.  
  93. =item EXISTS this, key
  94.  
  95. Verify that I<key> exists with the tied hash I<this>.
  96.  
  97. The B<Tie::Hash> implementation is a stub that simply croaks.
  98.  
  99. =item DELETE this, key
  100.  
  101. Delete the key I<key> from the tied hash I<this>.
  102.  
  103. =item CLEAR this
  104.  
  105. Clear all values from the tied hash I<this>.
  106.  
  107. =back
  108.  
  109. =head1 Inheriting from B<Tie::StdHash>
  110.  
  111. The accessor methods assume that the actual storage for the data in the tied
  112. hash is in the hash referenced by C<tied(%tiedhash)>.  Thus overwritten
  113. C<TIEHANDLE> method should return a hash reference, and the remaining methods
  114. should operate on the hash referenced by the first argument:
  115.  
  116.   package ReportHash;
  117.   our @ISA = 'Tie::StdHash';
  118.  
  119.   sub TIEHASH  {
  120.     my $storage = bless {}, shift;
  121.     warn "New ReportHash created, stored in $storage.\n";
  122.     $storage
  123.   }
  124.   sub STORE    {
  125.     warn "Storing data with key $_[1] at $_[0].\n";
  126.     $_[0]{$_[1]} = $_[2]
  127.   }
  128.  
  129.  
  130. =head1 Inheriting from B<Tie::ExtraHash>
  131.  
  132. The accessor methods assume that the actual storage for the data in the tied
  133. hash is in the hash referenced by C<(tied(%tiedhash))[0]>.  Thus overwritten
  134. C<TIEHANDLE> method should return an array reference with the first
  135. element being a hash reference, and the remaining methods should operate on the
  136. hash C<< %{ $_[0]->[0] } >>:
  137.  
  138.   package ReportHash;
  139.   our @ISA = 'Tie::StdHash';
  140.  
  141.   sub TIEHASH  {
  142.     my $storage = bless {}, shift;
  143.     warn "New ReportHash created, stored in $storage.\n";
  144.     [$storage, @_]
  145.   }
  146.   sub STORE    {
  147.     warn "Storing data with key $_[1] at $_[0].\n";
  148.     $_[0][0]{$_[1]} = $_[2]
  149.   }
  150.  
  151. The default C<TIEHANDLE> method stores "extra" arguments to tie() starting
  152. from offset 1 in the array referenced by C<tied(%tiedhash)>; this is the
  153. same storage algorithm as in TIEHASH subroutine above.  Hence, a typical
  154. package inheriting from B<Tie::ExtraHash> does not need to overwrite this
  155. method.
  156.  
  157. =head1 C<UNTIE> and C<DESTROY>
  158.  
  159. The methods C<UNTIE> and C<DESTROY> are not defined in B<Tie::Hash>,
  160. B<Tie::StdHash>, or B<Tie::ExtraHash>.  Tied hashes do not require
  161. presense of these methods, but if defined, the methods will be called in
  162. proper time, see L<perltie>.
  163.  
  164. If needed, these methods should be defined by the package inheriting from
  165. B<Tie::Hash>, B<Tie::StdHash>, or B<Tie::ExtraHash>.
  166.  
  167. =head1 MORE INFORMATION
  168.  
  169. The packages relating to various DBM-related implementations (F<DB_File>,
  170. F<NDBM_File>, etc.) show examples of general tied hashes, as does the
  171. L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
  172. good working examples.
  173.  
  174. =cut
  175.  
  176. use Carp;
  177. use warnings::register;
  178.  
  179. sub new {
  180.     my $pkg = shift;
  181.     $pkg->TIEHASH(@_);
  182. }
  183.  
  184. # Grandfather "new"
  185.  
  186. sub TIEHASH {
  187.     my $pkg = shift;
  188.     if (defined &{"${pkg}::new"}) {
  189.     warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing");
  190.     $pkg->new(@_);
  191.     }
  192.     else {
  193.     croak "$pkg doesn't define a TIEHASH method";
  194.     }
  195. }
  196.  
  197. sub EXISTS {
  198.     my $pkg = ref $_[0];
  199.     croak "$pkg doesn't define an EXISTS method";
  200. }
  201.  
  202. sub CLEAR {
  203.     my $self = shift;
  204.     my $key = $self->FIRSTKEY(@_);
  205.     my @keys;
  206.  
  207.     while (defined $key) {
  208.     push @keys, $key;
  209.     $key = $self->NEXTKEY(@_, $key);
  210.     }
  211.     foreach $key (@keys) {
  212.     $self->DELETE(@_, $key);
  213.     }
  214. }
  215.  
  216. # The Tie::StdHash package implements standard perl hash behaviour.
  217. # It exists to act as a base class for classes which only wish to
  218. # alter some parts of their behaviour.
  219.  
  220. package Tie::StdHash;
  221. # @ISA = qw(Tie::Hash);        # would inherit new() only
  222.  
  223. sub TIEHASH  { bless {}, $_[0] }
  224. sub STORE    { $_[0]->{$_[1]} = $_[2] }
  225. sub FETCH    { $_[0]->{$_[1]} }
  226. sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
  227. sub NEXTKEY  { each %{$_[0]} }
  228. sub EXISTS   { exists $_[0]->{$_[1]} }
  229. sub DELETE   { delete $_[0]->{$_[1]} }
  230. sub CLEAR    { %{$_[0]} = () }
  231.  
  232. package Tie::ExtraHash;
  233.  
  234. sub TIEHASH  { my $p = shift; bless [{}, @_], $p }
  235. sub STORE    { $_[0][0]{$_[1]} = $_[2] }
  236. sub FETCH    { $_[0][0]{$_[1]} }
  237. sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
  238. sub NEXTKEY  { each %{$_[0][0]} }
  239. sub EXISTS   { exists $_[0][0]->{$_[1]} }
  240. sub DELETE   { delete $_[0][0]->{$_[1]} }
  241. sub CLEAR    { %{$_[0][0]} = () }
  242.  
  243. 1;
  244.