Tie::RefHash(3perl)
π Tie::RefHash β the perl manual, bestie edition β¨
okay so you know how hashes are cute little key-value situations but the keys can only ever be boring strings? yeah. this is the fix. this is the Tie::RefHash page from the Perl Programmers Reference Guide, and it's about using references as hash keys. iconic behavior honestly πΈ
β¨ NAME β¨
Tie::RefHash β use references as hash keys π
π VERSION π
version 1.41 β yes she's been around a minute and she still slays
π¦ SYNOPSIS π¦
here's the whole vibe, copy it exactly bestie:
require 5.004;
use Tie::RefHash;
tie HASHVARIABLE, 'Tie::RefHash', LIST;
tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST;
untie HASHVARIABLE;
lowkey notice the require 5.004; at the top β she's not asking for much, just a perl from this century-ish, fr fr
π DESCRIPTION π
so here's the tea π΅: this module gives you the ability to use references as hash keys β but only if you tie the hash variable to this module first. no tie, no party, that's the rule.
normally? only the keys of the tied hash itself get preserved as references. so if you're doing hashes-of-hashes and you want references as keys in the inner hashes too, you need Tie::RefHash::Nestable β which ships as part of Tie::RefHash, so you don't even have to go looking for it. she's already in the group chat π«Ά
under the hood it's implemented using the standard perl TIEHASH interface. so if you want the lore, go read the "tie" entry in perlfunc(1) and also perltie(1). that's where the magic actually lives β¨
and the Nestable version? she's nosy in the best way: she works by looking for hash references being stored and then converting them into tied hashes, so that they too can have references as keys. it's giving "I fixed your whole data structure for you, you're welcome."
β οΈ wait, important, don't skip this: this happens without warning whenever you store a reference to one of your own hashes in the tied hash. not a drill, not a maybe β she just does it. so heads up π
π EXAMPLE π
okay this is the part where it all clicks. look:
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";
}
like. arrayref, hashref, globref, scalarref, coderef β all chilling as keys. all of them. main character energy honestly π
π§΅ THREAD SUPPORT π§΅
Tie::RefHash fully supports threading using the CLONE method. fully. no cap. π§
πΎ STORABLE SUPPORT πΎ
Storable hooks are provided for semantically correct serialization and cloning of tied refhashes. so your refhash travels well and comes back the same person. obsessed π
π SEE ALSO π
go read 'em, iykyk π
π SUPPORT π
found a bug? periodt, report it bestie. bugs may be submitted through the RT bug tracker β https://rt.cpan.org/Public/Dist/Display.html?Name=Tie-RefHash β or you can email bug-Tie-RefHash@rt.cpan.org (that's mailto:bug-Tie-RefHash@rt.cpan.org if you're being fancy). π
π AUTHORS π
- Gurusamy Sarathy
<gsar@activestate.com>β the original icon - Tie::RefHash::Nestable by Ed Avis
<ed@membled.com>β the nesting queen
π« CONTRIBUTORS π«
- Yuval Kogman
<nothingmuch@woobling.org> - Karen Etheridge
<ether@cpan.org> - Florian Ragwitz
<rafl@debian.org> - Lukas Mai
<lukasmai.403@gmail.com> - Jerry D. Hedden
<jdhedden@cpan.org> - tusooa
<tusooa@kazv.moe>
shoutout to every single one of y'all, this module literally would not be what it is without you π
π COPYRIGHT AND LICENCE π
This software is copyright (c) 2006 by ΧΧΧΧ Χ§ΧΧ'ΧΧ (Yuval Kogman) <nothingmuch@woobling.org>. π«Ά
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. free as in freedom, free as in go off π
cute little sign-off time π this whole page is from perl v5.44.0 and it was dated 2026-10-17 β and with that, Tie::RefHash(3perl) is a wrap. go tie something weird, bestie. you got this. ππΈβ¨
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms