TGI has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a group of OOP modules that will be used to track host status over time. There are three classes involved: Host, Event and HostGroup. The HostGroup class contains many Events and many Hosts (in actuality, subclassed versions of Host and Event). Host and Event are base classes that are intended to be subclassed, they should also have no persistance related code. I want to handle all persistance in class HostGroup.

This arrangement naturally leads to the question "If a Host/Event is not aware of persistance, how do I make sure that the stored copy is updated when I update the object in memory?" The solution I have arrived at is to have a HostGroup method, updateObject, that takes the object, method and the methods arguments as arguments:

sub updateObject { my $self = shift; my $object = shift; my $method = shift; my @returnValues = $object->$method(@_); # This method handles all updates to object store. $self->_storeObject($object); return @returnValues; }
Which seems to work and doesn't set strict screaming. Is there a problem with this approach that I am not seeing? My preliminary tests have come out fine....


TGI says moo

Replies are listed 'Best First'.
Re: Managing object persistance in a container class
by chromatic (Archbishop) on Jan 08, 2002 at 04:47 UTC
    Are you asking about using a scalar containing a method name?

    Method dispatch cannot be checked at compile time, as you can manipulate @ISA or use AUTOLOAD or eval or all sorts of other nice trickery. I wouldn't make a habit of such a thing if there were other ways to do it, but it looks okay in this case.

    You might want an eval block in there to catch any errors, though.

Re: Managing object persistance in a container class
by dmmiller2k (Chaplain) on Jan 08, 2002 at 20:17 UTC

    As chromatic says, it's not clear what you are asking here. Are you saying you want to do this:

    $hostGrp->updateObject( $object, 'do_something', @args );
    instead of this:
    $object->do_something( @args );
    so you can persist the internal effects of calling the $object->do_something() method?

    If so, it isn't clear exactly what's the question?

    dmm