Tie::Hash(3pm)
π NAME π
Hii bestie! Welcome to Tie::Hash(3pm), straight out of the Perl Programmers Reference Guide β¨ This page is about Tie::Hash, Tie::StdHash, and Tie::ExtraHash β the base class definitions for tied hashes. They're the scaffolding girlies you inherit from when you want a hash to act all custom and iconic π
β¨ SYNOPSIS β¨
Here's the exact code vibe from the page β same characters, same comments, no edits:
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 πΈ
Okay so this module hands you some skeletal methods for hash-tying classes. If you need the full list of functions required to tie a hash to a package, peek at perltie. The basic Tie::Hash package gives you a new method, plus the methods TIEHASH, EXISTS, and CLEAR.
Tie::StdHash and Tie::ExtraHash? They provide most methods for hashes described in perltie β the exceptions are UNTIE and DESTROY. They make tied hashes behave exactly like standard hashes and let you selectively overwrite methods. Also, Tie::Hash has legacy support for the new method: it's used if TIEHASH is not defined, in the case a class forgets to include a TIEHASH method π«Ά
For developers who want to write their own tied hashes, the required methods are briefly defined below. For more detailed descriptions and example code, see the perltie section:
TIEHASH classname, LIST
The method invoked by the command tie %hash, classname. It associates a new hash instance with the specified class. LIST would represent additional arguments (along the lines of AnyDBM_File 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 Tie::Hash 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.
Tie::Hash does not implement this method (but Tie::StdHash and Tie::ExtraHash do).
π¦ Inheriting from Tie::StdHash π¦
The accessor methods assume that the actual storage for the data in the tied hash is in the hash referenced by tied(%tiedhash). So an overwritten 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]
}
π§ Inheriting from Tie::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]. So an 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 the TIEHASH subroutine above. Hence, a typical package inheriting from Tie::ExtraHash does not need to overwrite this method.
π SCALAR", "UNTIE" and "DESTROY" π
The methods UNTIE and DESTROY are not defined in Tie::Hash, Tie::StdHash, or Tie::ExtraHash. Tied hashes do not require the presence of these methods, but if they are defined, the methods will be called in proper time β see perltie π°οΈ
SCALAR is only defined in Tie::StdHash and Tie::ExtraHash.
If needed, these methods should be defined by the package inheriting from Tie::Hash, Tie::StdHash, or Tie::ExtraHash. See "SCALAR" in perltie to find out what happens when "SCALAR" does not exist.
π MORE INFORMATION π
The packages relating to various DBM-related implementations (DB_File, NDBM_File, etc.) show examples of general tied hashes, and so does the Config module. While these do not utilize Tie::Hash, they serve as good working examples β¨
Alright bestie, that's the whole Tie::Hash(3pm) page! This one is from perl v5.42.3, dated 2026-11-03, and it's the Tie::Hash(3pm) manual. Now go tie those hashes with main character energy β mwah! ππβ¨
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms