Think about Loose Coupling | |
PerlMonks |
Overloading subs = weirdness in mod_perlby cfreak (Chaplain) |
on Nov 15, 2004 at 17:23 UTC ( [id://407882]=perlquestion: print w/replies, xml ) | Need Help?? |
cfreak has asked for the wisdom of the Perl Monks concerning the following question: Let me start off by saying what I'm doing probably isn't entirely sane, but hey, Perl is supposed to be for the insane right? :) I'm building a large shopping cart type app and I have a base set of modules. This app is going to be used by several customers and a few of them need some customizations. Since everything happens in modules, every site calls the same startup.pl and then loads a configuration based on PerlSetVar directives in their virtual host setup. The config can load custom modules which in some cases I've overloaded some subs that are located in my core modules. (I do this per request so as not to leave these overloads in memory thus breaking other sites). In most cases this has worked really well, however I've come to a function where one of the Apache::Request parameters needs to be changed. I reference the original sub to a scalar, then overload and then I call original sub through the reference after I've made my modifications. However when I modify the parameter it appears my scalar reference suddenly maps back to overloaded sub instead of the original, thus creating an infinate self referencing call (oops). Here's the offending code in question: (The Apache::Request object is stored as $self->{apr}
The only thing I can figure is that modifying the $self variable changes its type? It seems very odd to me. Has anyone else seen this behavior and if so how did you work around it? I know I could get around it by simply passing the IDs, but I'm trying to avoid modifying the core function since other site rely on its current behavior. Update Messing with the symbol table in a mod_perl environment has becoming an increasingly bad idea (tm). I fixed this problem using diotalevi's and bart's suggestions and learned something about OO as well :). If anyone's interested ... Basically my program was backwards. I had a core module that I'm was trying to include and then let it get objects for the needed underlying modules. After doing that I was trying to bolt my customizations on top (thus mucking with the symbol table). Well first of all with this approach, calling methods between modules was a real pain because I had to pass each new call a hash reference that contained the database handle, the Apache::Request handle, a configuration handle and some other stuff. Passing the object itself worked fine, however if I needed to return from one of those calls the object would be re-blessed into the new object and all my previous methods would go out of scope. The second problem was of course my original question, changing the symbols. This worked fine with a few requests but it appears I must have been getting answers from the same child of Apache. I believe upon getting a new child, creating a reference to a particular function would reference the newly created function by that name in memory and not the original one. This led to the infinate recursive call I talked about earlier. (hey crashing apache was fun!) Well what I learned is that when you subclass and call a method, Perl looks for that method in your subclass first and then goes looking for it in the base classes until it finds it. By turing everything around and calling my customized module from the handler and overriding the base classes in it, I can create my customizations. Calls between modules are fixed now too because instead of calling new each time and reblessing my object I just subclass those modules as well. Anyway I hope reading that helps someone else if they attempt a large OO project like mine :)
Back to
Seekers of Perl Wisdom
|
|