<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Tie::Hash;

our $VERSION = '1.06';

=head1 NAME

Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes

=head1 SYNOPSIS

    package NewHash;
    require Tie::Hash;

    @ISA = qw(Tie::Hash);

    sub DELETE { ... }		# Provides needed method
    sub CLEAR { ... }		# Overrides inherited method


    package NewStdHash;
    require Tie::Hash;

    @ISA = qw(Tie::StdHash);

    # All methods provided by default, define
    # only those needing overrides
    # Accessors access the storage in %{$_[0]};
    # TIEHASH should return a reference to the actual storage
    sub DELETE { ... }

    package NewExtraHash;
    require Tie::Hash;

    @ISA = qw(Tie::ExtraHash);

    # All methods provided by default, define 
    # only those needing overrides
    # Accessors access the storage in %{$_[0][0]};
    # TIEHASH should return an array reference with the first element
    # being the reference to the actual storage 
    sub DELETE { 
      $_[0][1]-&gt;('del', $_[0][0], $_[1]); # Call the report writer
      delete $_[0][0]-&gt;{$_[1]};		  #  $_[0]-&gt;SUPER::DELETE($_[1])
    }


    package main;

    tie %new_hash, 'NewHash';
    tie %new_std_hash, 'NewStdHash';
    tie %new_extra_hash, 'NewExtraHash',
	sub {warn "Doing \U$_[1]\E of $_[2].\n"};

=head1 DESCRIPTION

This module provides some skeletal methods for hash-tying classes. See
L&lt;perltie&gt; for a list of the functions required in order to tie a hash
to a package. The basic B&lt;Tie::Hash&gt; package provides a C&lt;new&gt; method, as well
as methods C&lt;TIEHASH&gt;, C&lt;EXISTS&gt; and C&lt;CLEAR&gt;. The B&lt;Tie::StdHash&gt; and
B&lt;Tie::ExtraHash&gt; packages
provide most methods for hashes described in L&lt;perltie&gt; (the exceptions
are C&lt;UNTIE&gt; and C&lt;DESTROY&gt;).  They cause tied hashes to behave exactly like standard hashes,
and allow for selective overwriting of methods.  B&lt;Tie::Hash&gt; has legacy support for the
C&lt;new&gt; method: it is used if C&lt;TIEHASH&gt; is not defined
in the case a class forgets to include a C&lt;TIEHASH&gt; method.

For developers wishing to write their own tied hashes, the required methods
are briefly defined below. See the L&lt;perltie&gt; section for more detailed
descriptive, as well as example code:

=over 4

=item TIEHASH classname, LIST

The method invoked by the command C&lt;tie %hash, classname&gt;. Associates a new
hash instance with the specified class. C&lt;LIST&gt; would represent additional
arguments (along the lines of L&lt;AnyDBM_File&gt; and compatriots) needed to
complete the association.

=item STORE this, key, value

Store datum I&lt;value&gt; into I&lt;key&gt; for the tied hash I&lt;this&gt;.

=item FETCH this, key

Retrieve the datum in I&lt;key&gt; for the tied hash I&lt;this&gt;.

=item FIRSTKEY this

Return the first key in the hash.

=item NEXTKEY this, lastkey

Return the next key in the hash.

=item EXISTS this, key

Verify that I&lt;key&gt; exists with the tied hash I&lt;this&gt;.

The B&lt;Tie::Hash&gt; implementation is a stub that simply croaks.

=item DELETE this, key

Delete the key I&lt;key&gt; from the tied hash I&lt;this&gt;.

=item CLEAR this

Clear all values from the tied hash I&lt;this&gt;.

=item SCALAR this

Returns what evaluating the hash in scalar context yields.

B&lt;Tie::Hash&gt; does not implement this method (but B&lt;Tie::StdHash&gt;
and B&lt;Tie::ExtraHash&gt; do).

=back

=head1 Inheriting from B&lt;Tie::StdHash&gt;

The accessor methods assume that the actual storage for the data in the tied
hash is in the hash referenced by C&lt;tied(%tiedhash)&gt;.  Thus overwritten
C&lt;TIEHASH&gt; method should return a hash reference, and the remaining methods
should operate on the hash referenced by the first argument:

  package ReportHash;
  our @ISA = 'Tie::StdHash';

  sub TIEHASH  {
    my $storage = bless {}, shift;
    warn "New ReportHash created, stored in $storage.\n";
    $storage
  }
  sub STORE    {
    warn "Storing data with key $_[1] at $_[0].\n";
    $_[0]{$_[1]} = $_[2]
  }


=head1 Inheriting from B&lt;Tie::ExtraHash&gt;

The accessor methods assume that the actual storage for the data in the tied
hash is in the hash referenced by C&lt;(tied(%tiedhash))-E&lt;gt&gt;[0]&gt;.  Thus overwritten
C&lt;TIEHASH&gt; method should return an array reference with the first
element being a hash reference, and the remaining methods should operate on the
hash C&lt;&lt; %{ $_[0]-&gt;[0] } &gt;&gt;:

  package ReportHash;
  our @ISA = 'Tie::ExtraHash';

  sub TIEHASH  {
    my $class = shift;
    my $storage = bless [{}, @_], $class;
    warn "New ReportHash created, stored in $storage.\n";
    $storage;
  }
  sub STORE    {
    warn "Storing data with key $_[1] at $_[0].\n";
    $_[0][0]{$_[1]} = $_[2]
  }

The default C&lt;TIEHASH&gt; method stores "extra" arguments to tie() starting
from offset 1 in the array referenced by C&lt;tied(%tiedhash)&gt;; this is the
same storage algorithm as in TIEHASH subroutine above.  Hence, a typical
package inheriting from B&lt;Tie::ExtraHash&gt; does not need to overwrite this
method.

=head1 C&lt;SCALAR&gt;, C&lt;UNTIE&gt; and C&lt;DESTROY&gt;

The methods C&lt;UNTIE&gt; and C&lt;DESTROY&gt; are not defined in B&lt;Tie::Hash&gt;,
B&lt;Tie::StdHash&gt;, or B&lt;Tie::ExtraHash&gt;.  Tied hashes do not require
presence of these methods, but if defined, the methods will be called in
proper time, see L&lt;perltie&gt;.

C&lt;SCALAR&gt; is only defined in B&lt;Tie::StdHash&gt; and B&lt;Tie::ExtraHash&gt;.

If needed, these methods should be defined by the package inheriting from
B&lt;Tie::Hash&gt;, B&lt;Tie::StdHash&gt;, or B&lt;Tie::ExtraHash&gt;. See L&lt;perltie/"SCALAR"&gt;
to find out what happens when C&lt;SCALAR&gt; does not exist.

=head1 MORE INFORMATION

The packages relating to various DBM-related implementations (F&lt;DB_File&gt;,
F&lt;NDBM_File&gt;, etc.) show examples of general tied hashes, as does the
L&lt;Config&gt; module. While these do not utilize B&lt;Tie::Hash&gt;, they serve as
good working examples.

=cut

use Carp;
use warnings::register;

sub new {
    my $pkg = shift;
    $pkg-&gt;TIEHASH(@_);
}

# Legacy support for new()

sub TIEHASH {
    my $pkg = shift;
    my $pkg_new = $pkg -&gt; can ('new');

    if ($pkg_new and $pkg ne __PACKAGE__) {
        my $my_new = __PACKAGE__ -&gt; can ('new');
        if ($pkg_new == $my_new) {  
            #
            # Prevent recursion
            #
            croak "$pkg must define either a TIEHASH() or a new() method";
        }

	warnings::warnif ("WARNING: calling ${pkg}-&gt;new since " .
                          "${pkg}-&gt;TIEHASH is missing");
	$pkg -&gt; new (@_);
    }
    else {
	croak "$pkg doesn't define a TIEHASH method";
    }
}

sub EXISTS {
    my $pkg = ref $_[0];
    croak "$pkg doesn't define an EXISTS method";
}

sub CLEAR {
    my $self = shift;
    my $key = $self-&gt;FIRSTKEY(@_);
    my @keys;

    while (defined $key) {
	push @keys, $key;
	$key = $self-&gt;NEXTKEY(@_, $key);
    }
    foreach $key (@keys) {
	$self-&gt;DELETE(@_, $key);
    }
}

# The Tie::StdHash package implements standard perl hash behaviour.
# It exists to act as a base class for classes which only wish to
# alter some parts of their behaviour.

package Tie::StdHash;
# @ISA = qw(Tie::Hash);		# would inherit new() only

sub TIEHASH  { bless {}, $_[0] }
sub STORE    { $_[0]-&gt;{$_[1]} = $_[2] }
sub FETCH    { $_[0]-&gt;{$_[1]} }
sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
sub NEXTKEY  { each %{$_[0]} }
sub EXISTS   { exists $_[0]-&gt;{$_[1]} }
sub DELETE   { delete $_[0]-&gt;{$_[1]} }
sub CLEAR    { %{$_[0]} = () }
sub SCALAR   { scalar %{$_[0]} }

package Tie::ExtraHash;

sub TIEHASH  { my $p = shift; bless [{}, @_], $p }
sub STORE    { $_[0][0]{$_[1]} = $_[2] }
sub FETCH    { $_[0][0]{$_[1]} }
sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
sub NEXTKEY  { each %{$_[0][0]} }
sub EXISTS   { exists $_[0][0]-&gt;{$_[1]} }
sub DELETE   { delete $_[0][0]-&gt;{$_[1]} }
sub CLEAR    { %{$_[0][0]} = () }
sub SCALAR   { scalar %{$_[0][0]} }

1;
</pre></body></html>