[Chicago-talk] super complex object

Steven Lembark lembark at wrkhors.com
Thu Aug 4 19:09:38 PDT 2005

> I really wish I understood anything you just said. I read over the
> documentation for  NEXT::init and still have no idea what it would be
> used for. I think it's the fact that  I'm self taught so don't understand
> a lot of the terminology. Sorry for my ignorance.

Say you want to have five classes that share data.
One way is to have them all stuff data into a single
object; problem there is the object gets bulky and
you get collisions between various uses of obvious

Alternative is to have a metadata hash in each
class' package that is used to hold data for
objects that the class has seen.

You start out with:

    my %meta = ();

as you see an object that the class cares about
you can:

    my $obj = shift;

    $meta{ $obj } = { @$default_meta };

The trick here is that any object has a unique
stringified version.

Tim Bunce uses this for DBI: the $dbh handed
back by DBI->connect( ... ) is an empty hash.
This never fails to freak out a few people per
week on the DBI mailing list.

You don't see the stuff he manages with DBI.
Instead of using

    my $foo = $obj->{foo};

he uses:

    my $objmeta = $meta{ $obj } ||= { @default_meta };

    my $foo = $objmeta->{ foo };

which keeps the metaadata private to the DBI
class and its minions.

If you have a huge object then you could probably
use this technique -- along with multiple inheritence --
to sequestedr the private information for each base
class in a private are where they don't collide.

If you snag a copy of Perl Best Pratices it goes
over how to do this -- with working examples.

Any performance hit you'll see from this technique
is on the order of microseconds per call; which
shouldn't be painful for anthing more recent thatn
an x486...

Steven Lembark                                       85-09 90th Street
Workhorse Computing                                Woodhaven, NY 11421
lembark at wrkhors.com                                     1 888 359 3508

More information about the Chicago-talk mailing list