Manual Pages for UNIX Darwin command on man Tie::Hash
MyWebUniversity

Manual Pages for UNIX Darwin command on man Tie::Hash

Tie::Hash(3pm) Perl Programmers Reference Guide Tie::Hash(3pm)

NAME

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

tied hashes

SYNOPSIS

package NewHash;

require Tie::Hash;

@ISA = (Tie::Hash);

sub DELETE { ... } # Provides needed method

sub CLEAR { ... } # Overrides inherited method

package NewStdHash;

require Tie::Hash;

@ISA = (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 = (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]->('del', $[0][0], $[1]); # Call the report writer

delete $[0][0]->{$[1]}; # $[0]->SUPER::DELETE($[1])

} package main;

tie %newhash, 'NewHash';

tie %newstdhash, 'NewStdHash';

tie %newextrahash, 'NewExtraHash',

sub {warn "Doing \U$[1]\E of $[2].\n"};

DESCRIPTION

This module provides some skeletal methods for hash-tying classes. See

perltie for a list of the functions required in order to tie a hash to a package. The basic TTiiee::::HHaasshh package provides a "new" method, as well as methods "TIEHASH", "EXISTS" and "CLEAR". The TTiiee::::SSttddHHaasshh and TTiiee::::EExxttrraaHHaasshh packages provide most methods for hashes described in perltie (the exceptions are "UNTIE" and "DESTROY"). They cause tied hashes to behave exactly like standard hashes, and allow for selective overwriting of methods. TTiiee::::HHaasshh grandfathers the "new" method: it is used if "TIEHASH" is not defined in the case a class forgets to include a "TIEHASH" method. For developers wishing to write their own tied hashes, the required methods are briefly defined below. See the perltie section for more detailed descriptive, as well as example code: TIEHASH classname, LIST

The method invoked by the command "tie %hash, classname". Asso-

ciates a new hash instance with the specified class. "LIST" would represent additional arguments (along the lines of AnyDBMFile and compatriots) needed to complete the association. STORE this, key, value Store datum value into key for the tied hash this. FETCH this, key Retrieve the datum in key for the tied hash this. FIRSTKEY this Return the first key in the hash. NEXTKEY this, lastkey Return the next key in the hash. EXISTS this, key Verify that key exists with the tied hash this. The TTiiee::::HHaasshh implementation is a stub that simply croaks. DELETE this, key Delete the key key from the tied hash this. CLEAR this Clear all values from the tied hash this. SCALAR this Returns what evaluating the hash in scalar context yields. TTiiee::::HHaasshh does not implement this method (but TTiiee::::SSttddHHaasshh and TTiiee::::EExxttrraaHHaasshh do). IInnhheerriittiinngg ffrroomm TTiiee::::SSttddHHaasshh The accessor methods assume that the actual storage for the data in the

tied hash is in the hash referenced by "tied(%tiedhash)". Thus over-

written "TIEHASH" 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]

} IInnhheerriittiinngg ffrroomm TTiiee::::EExxttrraaHHaasshh The accessor methods assume that the actual storage for the data in the

tied hash is in the hash referenced by "(tied(%tiedhash))->[0]". Thus

overwritten "TIEHASH" method should return an array reference with the first element being a hash reference, and the remaining methods should

operate on the hash "%{ $[0]->[0] }":

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 "TIEHASH" method stores "extra" arguments to tie() starting

from offset 1 in the array referenced by "tied(%tiedhash)"; this is the

same storage algorithm as in TIEHASH subroutine above. Hence, a typi-

cal package inheriting from TTiiee::::EExxttrraaHHaasshh does not need to overwrite this method. ""SSCCAALLAARR"", "UNTIE" and "DESTROY" The methods "UNTIE" and "DESTROY" are not defined in TTiiee::::HHaasshh, TTiiee::::SSttddHHaasshh, or TTiiee::::EExxttrraaHHaasshh. Tied hashes do not require presence of these methods, but if defined, the methods will be called in proper time, see perltie. "SCALAR" is only defined in TTiiee::::SSttddHHaasshh and TTiiee::::EExxttrraaHHaasshh. If needed, these methods should be defined by the package inheriting from TTiiee::::HHaasshh, TTiiee::::SSttddHHaasshh, or TTiiee::::EExxttrraaHHaasshh. See "SCALAR" in pertie to find out what happens when "SCALAR" does not exist. MMOORREE IINNFFOORRMMAATTIIOONN

The packages relating to various DBM-related implementations (DBFile,

NDBMFile, etc.) show examples of general tied hashes, as does the Con-

fig module. While these do not utilize TTiiee::::HHaasshh, they serve as good working examples.

perl v5.8.8 2001-09-21 Tie::Hash(3pm)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™