# perldoc > Tie::Hash

---
type: CommandReference
command: Tie::Hash
mode: perldoc
section: 
source: perldoc
---

## Quick Reference
- `tie %hash, 'NewHash'` — tie a hash to a class using Tie::Hash
- `tie %hash, 'NewStdHash'` — tie a hash to a class using Tie::StdHash with default methods
- `tie %hash, 'NewExtraHash', \&callback` — tie a hash with extra arguments
- `use Tie::Hash; @ISA = qw(Tie::Hash);` — inherit base methods
- `use Tie::StdHash; @ISA = qw(Tie::StdHash);` — inherit all standard hash methods
- `use Tie::ExtraHash; @ISA = qw(Tie::ExtraHash);` — inherit with extra storage

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

## Synopsis
perl
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; override only needed methods.
# Accessors access storage in %{$_[0]};
# TIEHASH should return a reference to the actual storage.
sub DELETE { ... }

package NewExtraHash;
require Tie::Hash;
@ISA = qw(Tie::ExtraHash);
# Accessors access storage in %{$_[0][0]};
# TIEHASH should return an array reference with first element being the actual storage reference.
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"};
## Options (Methods)
- `TIEHASH classname, LIST` — Method invoked by `tie %hash, classname`. Associates a new hash instance with the class. LIST is additional arguments.
- `STORE this, key, value` — Store datum *value* into *key* for the tied hash.
- `FETCH this, key` — Retrieve datum in *key* for the tied hash.
- `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. The Tie::Hash implementation is a stub that croaks.
- `DELETE this, key` — Delete key *key* from the tied hash.
- `CLEAR this` — Clear all values from the tied hash.
- `SCALAR this` — Returns what evaluating the hash in scalar context yields. Not implemented in Tie::Hash; implemented in Tie::StdHash and Tie::ExtraHash.
- `UNTIE` and `DESTROY` — Not defined in any of these classes; if defined in subclass, they are called as per perltie.

## Examples
### Inheriting from Tie::StdHash
perl
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
perl
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]
}
## See Also
- [perltie](http://localhost/phpMan.php/perldoc/perltie/markdown) — full documentation for tied hashes and other tie types.
- [DB_File](http://localhost/phpMan.php/perldoc/DB_File/markdown), [NDBM_File](http://localhost/phpMan.php/perldoc/NDBM_File/markdown) — examples of tied hash implementations.
- [Config](http://localhost/phpMan.php/perldoc/Config/markdown) — another example of tied hash.

## Exit Codes
(Not documented)