# perldoc > Tie::RefHash

---
type: CommandReference
command: Tie::RefHash
mode: perldoc
section: "3"
source: perldoc
---

## Quick Reference

- `tie %hash, 'Tie::RefHash'` — enable use of references as keys in `%hash`
- `tie %hash, 'Tie::RefHash::Nestable'` — allow references as keys in nested hashes-of-hashes
- `$a = []; $b = {}; %h = ($a => 1, $b => 2)` — store references as keys
- `for (keys %h) { print ref($_) }` — iterate over keys, printing their reference type
- `$h{$a}->{$b} = 1` — with Nestable, create nested tied hash automatically

## Name

**Tie::RefHash** — Use references as hash keys

## Synopsis

perl
require 5.004;
use Tie::RefHash;
tie HASHVARIABLE, 'Tie::RefHash', LIST;
tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST;
untie HASHVARIABLE;
## Description

This module provides the ability to use references as hash keys by first tying the hash variable to it. Normally, only the keys of the tied hash itself are preserved as references. To use references as keys in hashes-of-hashes, use `Tie::RefHash::Nestable`, which automatically converts stored hash references to tied hashes.

**Thread support:** Fully supports threading via the `CLONE` method.

**Storable support:** Hooks are provided for semantically correct serialization and cloning of tied refhashes.

## Examples

perl
use Tie::RefHash;
tie %h, 'Tie::RefHash';
$a = [];
$b = {};
$c = \*main;
$d = \"gunk";
$e = sub { 'foo' };
%h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5);
$a->[0] = 'foo';
$b->{foo} = 'bar';
for (keys %h) {
   print ref($_), "\n";
}

tie %h, 'Tie::RefHash::Nestable';
$h{$a}->{$b} = 1;
for (keys %h, keys %{$h{$a}}) {
   print ref($_), "\n";
}
## See Also

- [perl](https://perldoc.perl.org/perl) — Perl overview and documentation