| [reply] |
Quite possibly.
The basic class hierarchy I'm working with makes good design sense and is essentially a tree of classes with one base class that is subclassed one or more times, each of those is subclassed zero or more times, plus another layer or two of subclasses beyond that on some branches. Currently there is no multiple inheritance (though I don't think I can completely rule that out for the future). I also can't rule out the possibility that the base class won't someday be a subclass of another class (such as a standard Perl class of some kind).
Each class in the hierarchy has a hash variable that contains a set of values that add to and/or overload the same set of values from the parent class. I'd like to have a function in the parent class that creates a merged hash and caches that value (on the first call) or returns the cached value (on subsequent calls). I'd rather not copy a lot of code to each class to do the merging and caching in each class.
I have the above working just fine when called at runtime, but if I try to create an instance of the base class as part of module compilation (i.e., my $obj = classname->new(...) code outside of any sub declaration, not using a BEGIN block), it doesn't work. (I suspect it has something to do with the order in which variables are declared and/or initialized.) Hence, I am changing it to create the required object the first time it needs it rather than at compile time. As this is a one-time step, any costs involved in eval (which I've now removed because it wasn't needed) or autoload is negligible in this case. Even so, I'm trying to minimize the impact so that I can use this as a generalized auto-vivification and/or delayed initialization solution in the future.
| [reply] [d/l] |
... but if I try to create an instance of the base class as part of module compilation ...
Considering that seems to cause the problem, I'm curious as to why; what do you need that instance for?
... it doesn't work. (I suspect it has something to do with the order in which variables are declared and/or initialized.)
Tracking down the source of that problem may prove insightful!
The best thing to get people to take a look at it would be if you would provide a short, self-contained piece of example code that reproduces the problem (see http://sscce.org/).
| [reply] [d/l] |
Although I don't want to discourage the development of a solution if it works, is tested, and is useful, I tend to agree that it sounds like your problem might have a more direct solution, by updating the OO design. If a parent class needs access to a subclass's property, an abstract accessor is a solution, and if a subclass needs access to a parent, then an accessor should do. As for a class offering an instance of itself, maybe a short example would help to see what you're doing. Maybe there is some other thing that could be delayed from initialization time to runtime whose solution would come more naturally. | [reply] |
The problem is not how to get to the data. The problem is when to get to the data. Even with accessors and abstract accessors, I need to defer until compilation of the modules is complete before I try to use the accessors. That's why I was looking for a way to defer initialization and came up with the automaic vivification method I started with.
| [reply] |