package Hash::Util::FieldHash;
use 5.009004;
use strict;
use warnings;
use Scalar::Util qw( reftype);
our $VERSION = '1.04';
require Exporter;
our @ISA = qw(Exporter);
our %EXPORT_TAGS = (
'all' => [ qw(
fieldhash
fieldhashes
idhash
idhashes
id
id_2obj
register
)],
);
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
{
require XSLoader;
my %ob_reg; # private object registry
sub _ob_reg { \ %ob_reg }
XSLoader::load('Hash::Util::FieldHash', $VERSION);
}
sub fieldhash (\%) {
for ( shift ) {
return unless ref() && reftype( $_) eq 'HASH';
return $_ if Hash::Util::FieldHash::_fieldhash( $_, 0);
return $_ if Hash::Util::FieldHash::_fieldhash( $_, 2) == 2;
return;
}
}
sub idhash (\%) {
for ( shift ) {
return unless ref() && reftype( $_) eq 'HASH';
return $_ if Hash::Util::FieldHash::_fieldhash( $_, 0);
return $_ if Hash::Util::FieldHash::_fieldhash( $_, 1) == 1;
return;
}
}
sub fieldhashes { map &fieldhash( $_), @_ }
sub idhashes { map &idhash( $_), @_ }
1;
__END__
=head1 NAME
Hash::Util::FieldHash - Support for Inside-Out Classes
=head1 SYNOPSIS
### Create fieldhashes
use Hash::Util qw(fieldhash fieldhashes);
# Create a single field hash
fieldhash my %foo;
# Create three at once...
fieldhashes \ my(%foo, %bar, %baz);
# ...or any number
fieldhashes @hashrefs;
### Create an idhash and register it for garbage collection
use Hash::Util::FieldHash qw(idhash register);
idhash my %name;
my $object = \ do { my $o };
# register the idhash for garbage collection with $object
register($object, \ %name);
# the following entry will be deleted when $object goes out of scope
$name{$object} = 'John Doe';
### Register an ordinary hash for garbage collection
use Hash::Util::FieldHash qw(id register);
my %name;
my $object = \ do { my $o };
# register the hash %name for garbage collection of $object's id
register $object, \ %name;
# the following entry will be deleted when $object goes out of scope
$name{id $object} = 'John Doe';
=head1 FUNCTIONS
C offers a number of functions in support of
L of class construction.
=over
=item id
id($obj)
Returns the reference address of a reference $obj. If $obj is
not a reference, returns $obj.
This function is a stand-in replacement for
L, that is, it returns
the reference address of its argument as a numeric value. The only
difference is that C returns C when given a
non-reference while C returns its argument unchanged.
C also uses a caching technique that makes it faster when
the id of an object is requested often, but slower if it is needed
only once or twice.
=item id_2obj
$obj = id_2obj($id)
If C<$id> is the id of a registered object (see L), returns
the object, otherwise an undefined value. For registered objects this
is the inverse function of C.
=item register
register($obj)
register($obj, @hashrefs)
In the first form, registers an object to work with for the function
C. In the second form, it additionally marks the given
hashrefs down for garbage collection. This means that when the object
goes out of scope, any entries in the given hashes under the key of
C will be deleted from the hashes.
It is a fatal error to register a non-reference $obj. Any non-hashrefs
among the following arguments are silently ignored.
It is I an error to register the same object multiple times with
varying sets of hashrefs. Any hashrefs that are not registered yet
will be added, others ignored.
Registry also implies thread support. When a new thread is created,
all references are replaced with new ones, including all objects.
If a hash uses the reference address of an object as a key, that
connection would be broken. With a registered object, its id will
be updated in all hashes registered with it.
=item idhash
idhash my %hash
Makes an idhash from the argument, which must be a hash.
An I works like a normal hash, except that it stringifies a
I differently. A reference is stringified
as if the C function had been invoked on it, that is, its
reference address in decimal is used as the key.
=item idhashes
idhashes \ my(%hash, %gnash, %trash)
idhashes \ @hashrefs
Creates many idhashes from its hashref arguments. Returns those
arguments that could be converted or their number in scalar context.
=item fieldhash
fieldhash %hash;
Creates a single fieldhash. The argument must be a hash. Returns
a reference to the given hash if successful, otherwise nothing.
A I is, in short, an idhash with auto-registry. When an
object (or, indeed, any reference) is used as a fieldhash key, the
fieldhash is automatically registered for garbage collection with
the object, as if C had been called.
=item fieldhashes
fieldhashes @hashrefs;
Creates any number of field hashes. Arguments must be hash references.
Returns the converted hashrefs in list context, their number in scalar
context.
=back
=head1 DESCRIPTION
A word on terminology: I shall use the term I for a scalar
piece of data that a class associates with an object. Other terms that
have been used for this concept are "object variable", "(object) property",
"(object) attribute" and more. Especially "attribute" has some currency
among Perl programmer, but that clashes with the C pragma. The
term "field" also has some currency in this sense and doesn't seem
to conflict with other Perl terminology.
In Perl, an object is a blessed reference. The standard way of associating
data with an object is to store the data inside the object's body, that is,
the piece of data pointed to by the reference.
In consequence, if two or more classes want to access an object they
I agree on the type of reference and also on the organization of
data within the object body. Failure to agree on the type results in
immediate death when the wrong method tries to access an object. Failure
to agree on data organization may lead to one class trampling over the
data of another.
This object model leads to a tight coupling between subclasses.
If one class wants to inherit from another (and both classes access
object data), the classes must agree about implementation details.
Inheritance can only be used among classes that are maintained together,
in a single source or not.
In particular, it is not possible to write general-purpose classes
in this technique, classes that can advertise themselves as "Put me
on your @ISA list and use my methods". If the other class has different
ideas about how the object body is used, there is trouble.
For reference L in L shows the standard implementation of
a simple class C in the well-known hash based way. It also demonstrates
the predictable failure to construct a common subclass C
of C and the class C (whose objects I be globrefs).
Thus, techniques are of interest that store object data I in
the object body but some other place.
=head2 The Inside-out Technique
With I classes, each class declares a (typically lexical)
hash for each field it wants to use. The reference address of an
object is used as the hash key. By definition, the reference address
is unique to each object so this guarantees a place for each field that
is private to the class and unique to each object. See L in
L for a simple example.
In comparison to the standard implementation where the object is a
hash and the fields correspond to hash keys, here the fields correspond
to hashes, and the object determines the hash key. Thus the hashes
appear to be turned I.
The body of an object is never examined by an inside-out class, only
its reference address is used. This allows for the body of an actual
object to be I while the object methods of the class
still work as designed. This is a key feature of inside-out classes.
=head2 Problems of Inside-out
Inside-out classes give us freedom of inheritance, but as usual there
is a price.
Most obviously, there is the necessity of retrieving the reference
address of an object for each data access. It's a minor inconvenience,
but it does clutter the code.
More important (and less obvious) is the necessity of garbage
collection. When a normal object dies, anything stored in the
object body is garbage-collected by perl. With inside-out objects,
Perl knows nothing about the data stored in field hashes by a class,
but these must be deleted when the object goes out of scope. Thus
the class must provide a C method to take care of that.
In the presence of multiple classes it can be non-trivial
to make sure that every relevant destructor is called for
every object. Perl calls the first one it finds on the
inheritance tree (if any) and that's it.
A related issue is thread-safety. When a new thread is created,
the Perl interpreter is cloned, which implies that all reference
addresses in use will be replaced with new ones. Thus, if a class
tries to access a field of a cloned object its (cloned) data will
still be stored under the now invalid reference address of the
original in the parent thread. A general C method must
be provided to re-establish the association.
=head2 Solutions
C addresses these issues on several
levels.
The C function is provided in addition to the
existing C. Besides its short name
it can be a little faster under some circumstances (and a
bit slower under others). Benchmark if it matters. The
working of C also allows the use of the class name
as a I as described L.
The C function is incorporated in I in the sense
that it is called automatically on every key that is used with
the hash. No explicit call is necessary.
The problems of garbage collection and thread safety are both
addressed by the function C. It registers an object
together with any number of hashes. Registry means that when the
object dies, an entry in any of the hashes under the reference
address of this object will be deleted. This guarantees garbage
collection in these hashes. It also means that on thread
cloning the object's entries in registered hashes will be
replaced with updated entries whose key is the cloned object's
reference address. Thus the object-data association becomes
thread-safe.
Object registry is best done when the object is initialized
for use with a class. That way, garbage collection and thread
safety are established for every object and every field that is
initialized.
Finally, I incorporate all these functions in one
package. Besides automatically calling the C function
on every object used as a key, the object is registered with
the field hash on first use. Classes based on field hashes
are fully garbage-collected and thread safe without further
measures.
=head2 More Problems
Another problem that occurs with inside-out classes is serialization.
Since the object data is not in its usual place, standard routines
like C, C and
C can't deal with it on their own. Both
C and C provide the necessary hooks to
make things work, but the functions or methods used by the hooks
must be provided by each inside-out class.
A general solution to the serialization problem would require another
level of registry, one that that associates I and fields.
So far, the functions of C are unaware of
any classes, which I consider a feature. Therefore C
doesn't address the serialization problems.
=head2 The Generic Object
Classes based on the C function (and hence classes based on
C and C) show a peculiar behavior in that
the class name can be used like an object. Specifically, methods
that set or read data associated with an object continue to work as
class methods, just as if the class name were an object, distinct from
all other objects, with its own data. This object may be called
the I of the class.
This works because field hashes respond to keys that are not references
like a normal hash would and use the string offered as the hash key.
Thus, if a method is called as a class method, the field hash is presented
with the class name instead of an object and blithely uses it as a key.
Since the keys of real objects are decimal numbers, there is no
conflict and the slot in the field hash can be used like any other.
The C function behaves correspondingly with respect to non-reference
arguments.
Two possible uses (besides ignoring the property) come to mind.
A singleton class could be implemented this using the generic object.
If necessary, an C method could die or ignore calls with
actual objects (references), so only the generic object will ever exist.
Another use of the generic object would be as a template. It is
a convenient place to store class-specific defaults for various
fields to be used in actual object initialization.
Usually, the feature can be entirely ignored. Calling I