|
|||||||||
NAMETie::SentientHash - Perl module implementing intelligent objects
SYNOPSIS
use Tie::SentientHash;
$hashref = new Tie::SentientHash $meta_data, $initial_data; $untiedhash = $hashref->export; $metadata = $hashref->_metadata;
$hashref->{key} = 'value'; $hashref->{key1}{key2} = $value; $value2 = $hashref->{key}; undef $hashref;
DESCRIPTION
The
References to scalars, arrays and hashes can be stored in hash elements in which case the referenced object is tied to an internal class of the appropriate type (Tie::SentientHash::NestedHash, ::NestedArray or ::NestedScalar), so that changes to the nested data structures can be tracked. The constructor is invoked with two hash references: the first contains metadata and the second the initial data values. The metadata hash may contain the following flags:
Trying to change an object in a way that is forbidden by the metadata will cause the module to croak. Changes are only tracked at the top level.
EXAMPLEI use Tie::SentientHash as the basis for implementing persistent objects in my CGI/mod_perl scripts. The details of reading and writing the objects from and to the database is handled by a class, but neither the class nor the high level code needs to keep track of whether the object has been changed in any way. For example if you had a pay per view system of some kind you could have a script that contained the following fragment:
sub pay_per_view ($$) { my($cust_id, $cost) = @_;
my $cust = load Customer $cust_id; $cust->{CREDIT} -= $cost; }
The customer object would be implemented in a module sketched out below. A
commit function is specified on the call to create a new sentient object,
and that function will be called when
package Customer;
sub load ($$) { my ($class, $cust_id) = @_; my $data = {};
# read customer data from a database into $data
my $meta = { COMMIT_SUB => \&_commit, READONLY => [ qw( CUST_ID ) ], FORBID_INSERTS => 1 };
return bless new Tie::SentientHash($meta, $data), $class; }
sub _commit ($$) { my ($meta, $data) = @_;
# As we have been called, something has changed. The names of # the modified fields are the keys of $meta->{MODIFIED}. We had # better write the data back out to the database.
RESTRICTIONSFull array semantics are only supported for Perl version 5.005.
AUTHORAndrew Ford <[email protected]>
SEE ALSO
COPYRIGHTCopyright 1999 Ford & Mason Ltd. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
||||||
Copyright © 1996-2002 Ford & Mason Ltd |