# Hash::Util::FieldHash
📦 std
Support for inside-out classes: hashes keyed by the identity of a reference, whose entries go away with the object. A *field hash* takes a reference as a key and stores it under the reference’s id, so the key survives stringification overloads and re-blessing. When the object is destroyed, its entry disappears from every field hash it was used in. That is the bookkeeping an inside-out class needs: each attribute is one field hash, keyed by the object. An *idhash* only replaces a reference key by its id; registering the object with `register` adds the garbage collection. ## Synopsis ```perl use Hash::Util::FieldHash qw(fieldhash id id_2obj register idhash); package Person; fieldhash my %name; sub new { my $self = bless \ do { my $o }, shift; $name{$self} = shift; $self } sub name { $name{$_[0]} } package main; my $p = Person->new('Ada'); print $p->name, "\n"; # Ada my $id = id($p); # the key %name stores it under print id_2obj($id) == $p ? "same\n" : "other\n"; undef $p; # %name is empty again ``` ## Functions `fieldhash(%h)` and `fieldhashes(\%a, \%b, ...)` turn hashes into field hashes; `idhash` and `idhashes` turn them into idhashes. Each returns a reference to the hash, or nothing when the argument is not a hash. A hash that already is one of the two keeps its kind. `id($ref)` is the id of a reference, the number a field hash stores it under; a non-reference comes back unchanged. `id_2obj($id)` is the registered object with that id, or `undef`. `register($obj, @hashrefs)` registers `$obj` and marks each hash in `@hashrefs` for the cleanup of its id; it returns a new reference to `$obj`. This is a native implementation of the XS half of Hash::Util::FieldHash 1.27; its `Hash/Util/FieldHash.pm` is used unchanged. Nothing is exported by default. ## Differences from upstream None. ## Functions ### Field hashes #### [`register`](FieldHash/register.md) Register an object for garbage collection in the given hashes. ### Object ids #### [`id`](FieldHash/id.md) The id of a reference: the number a field hash stores it under. #### [`id_2obj`](FieldHash/id_2obj.md) The registered object that has a given id.