Movatterモバイル変換


[0]ホーム

URL:


 / 
perl-5.40.1
River stage five • 11893 direct dependents • 33358 total dependents
/Tie::Hash

NAME

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

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]->('del', $_[0][0], $_[1]); # Call the report writer      delete $_[0][0]->{$_[1]};  #  $_[0]->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"};

DESCRIPTION

This module provides some skeletal methods for hash-tying classes. Seeperltie for a list of the functions required in order to tie a hash to a package. The basicTie::Hash package provides anew method, as well as methodsTIEHASH,EXISTS andCLEAR. TheTie::StdHash andTie::ExtraHash packages provide most methods for hashes described inperltie (the exceptions areUNTIE andDESTROY). They cause tied hashes to behave exactly like standard hashes, and allow for selective overwriting of methods.Tie::Hash has legacy support for thenew method: it is used ifTIEHASH is not defined in the case a class forgets to include aTIEHASH method.

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

TIEHASH classname, LIST

The method invoked by the commandtie %hash, classname. Associates a new hash instance with the specified class.LIST would represent additional arguments (along the lines ofAnyDBM_File and compatriots) needed to complete the association.

STORE this, key, value

Store datumvalue intokey for the tied hashthis.

FETCH this, key

Retrieve the datum inkey for the tied hashthis.

FIRSTKEY this

Return the first key in the hash.

NEXTKEY this, lastkey

Return the next key in the hash.

EXISTS this, key

Verify thatkey exists with the tied hashthis.

TheTie::Hash implementation is a stub that simply croaks.

DELETE this, key

Delete the keykey from the tied hashthis.

CLEAR this

Clear all values from the tied hashthis.

SCALAR this

Returns what evaluating the hash in scalar context yields.

Tie::Hash does not implement this method (butTie::StdHash andTie::ExtraHash do).

Inheriting fromTie::StdHash

The accessor methods assume that the actual storage for the data in the tied hash is in the hash referenced bytied(%tiedhash). Thus overwrittenTIEHASH 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]}

Inheriting fromTie::ExtraHash

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 overwrittenTIEHASH 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 defaultTIEHASH method stores "extra" arguments to tie() starting from offset 1 in the array referenced bytied(%tiedhash); this is the same storage algorithm as in TIEHASH subroutine above. Hence, a typical package inheriting fromTie::ExtraHash does not need to overwrite this method.

SCALAR,UNTIE andDESTROY

The methodsUNTIE andDESTROY are not defined inTie::Hash,Tie::StdHash, orTie::ExtraHash. Tied hashes do not require presence of these methods, but if defined, the methods will be called in proper time, seeperltie.

SCALAR is only defined inTie::StdHash andTie::ExtraHash.

If needed, these methods should be defined by the package inheriting fromTie::Hash,Tie::StdHash, orTie::ExtraHash. See"SCALAR" in perltie to find out what happens whenSCALAR does not exist.

MORE INFORMATION

The packages relating to various DBM-related implementations (DB_File,NDBM_File, etc.) show examples of general tied hashes, as does theConfig module. While these do not utilizeTie::Hash, they serve as good working examples.

Module Install Instructions

To install less, copy and paste the appropriate command in to your terminal.

cpanm

cpanm less

CPAN shell

perl -MCPAN -e shellinstall less

For more information on module installation, please visitthe detailed CPAN module installation guide.

Keyboard Shortcuts

Global
sFocus search bar
?Bring up this help dialog
GitHub
gpGo to pull requests
gigo to github issues (only if github is preferred repository)
POD
gaGo to author
gcGo to changes
giGo to issues
gdGo to dist
grGo to repository/SCM
gsGo to source
gbGo to file browse
Search terms
module: (e.g.module:Plugin)
distribution: (e.g.distribution:Dancer auth)
author: (e.g.author:SONGMU Redis)
version: (e.g.version:1.00)

[8]ページ先頭

©2009-2025 Movatter.jp