Sounds like you might want Moose roles. You could change the base classes you have into roles (pretty trivial), then use 'before/after/around' to modify the behaviour of the 'save' method appropriately. | [reply] |
As already noted, no, you can't bless a reference into more than one class, but this sounds like it has strong potential as a use case for Moose roles. @ISA is the only form of inheritance (single or multiple) recognized by the Perl interpreter.
Now, as for why you can't bless a reference into more than one class, you need to think about how Perl OO works in the first place:
A Perl "object" is actually just a reference which is associated (via bless) with the namespace of a certain package. (This package is commonly referred to as a "class", but, really, it's just a namespace.) This association tells perl that any subroutines accessed by way of the blessed reference should be resolved within the connected package's namespace, passing the reference itself as the first argument. You can't be in more than one namespace at a time, therefore a reference can't be blessed into more than one "class" at a time.
One way that you might be able to do what you're thinking about ("might" because I don't completely follow your intent) within normal Perl OO would be to access the inherited methods directly by their packages. There's not really anything special about SUPER, so if you have something that's both a Vehicle::Car and a Vehicle::Airplane, you can save it to both classes by calling both Vehicle::Car::save($self) and Vehicle::Airplane::save($self). | [reply] [d/l] [select] |
To slightly elaborate on my problem:
All my classes have a save() method which save the class-specific attributes to the database. Subclasses call SUPER::save() in this method and only handle their own, additional, attributes.
Now I need a way to
a. inherit the attributes of two different subclasses into a single class (composed of two arbitrary combinations of subclasses; e.g. a Vehicle that is a Vehicle::Car AND a Vehicle::Airplane)
b. make a save() call to this combined subclass that calls BOTH subclasses's save() methods. This will call SUPER::save() twice, but I can live with that.
Is there a way to do this dynamically, short of writing a package that inherits from both subclasses for every possible combination of two subclasses? | [reply] |
| [reply] [d/l] |
You'd be better off using the database to define your relationships. | [reply] |