Tie::SubstrHash(3pm)

✨ NAME ✨

Okay so bestie, welcome to Tie::SubstrHash β€” the Perl module that does fixed-table-size, fixed-key-length hashing. That's the whole vibe. It's a hash. But make it rigid. πŸ’…

This page lives in the Perl Programmers Reference Guide and honestly? It's a little gem.


🌸 SYNOPSIS 🌸

    require Tie::SubstrHash;

    tie %myhash, 'Tie::SubstrHash', $key_len, $value_len, $table_size;

That's it. Three little numbers and you're tied. πŸŽ€


πŸ’– DESCRIPTION πŸ’–

So the Tie::SubstrHash package gives you a hash-table-like interface to an array of determinate size β€” with a constant key size and a constant record size. It's giving structured. It's giving locked-in. It's giving no surprises.

When you tie a new hash to this package, you (the developer, the main character) have to specify three things:

  • the size of the keys that will be used,
  • the size of the value fields that the keys will index,
  • and the size of the overall table β€” and that's in terms of key-value pairs, not size in hard memory, iykyk.

These values will not change for the duration of the tied hash. Periodt. Once it's set, it's set.

Your newly-allocated hash table can now have data stored and retrieved. Slay. 🫢 But listen β€” there are rules, and they are strict:

Store more than $table_size elements? Fatal error. 😭 Store a value that isn't exactly $value_len characters long? Fatal error. πŸ’€ Reference through a key that isn't exactly $key_len characters long? Fatal error. πŸ”₯

Like yes, these constraints may seem excessive β€” lowkey they are β€” but the result is a hash table that uses much less internal memory than an equivalent freely-allocated hash table. So like… it's a trade-off bestie. Rigidity in exchange for efficiency. Iconic behaviour honestly. πŸ‘‘


πŸ¦‹ CAVEATS πŸ¦‹

Now for the fine print, and you know we have to talk about it:

  • Because the current implementation uses the table and key sizes for the hashing algorithm, there is no means by which to dynamically change the value of any of the initialization parameters. So don't even try to glow-up those numbers mid-flight. πŸ™ˆ
  • The hash does not support exists(). Nope. Not a thing. Don't come for her. πŸ’€
fr fr, remember: exists() is off the table. Plan accordingly.

With love and zero tolerance for wrong-length keys, this was page Tie::SubstrHash from perl v5.42.3, dated 2026-11-03 β€” see you in the group chat, bestie πŸŒˆβœ¨πŸ’œ

πŸ“¦ where this page comes from