I am trying to manage a situation in which several perl modules may have the same package name. That is, imagine the following situation. I have a module Foo defined like:
package Foo;
sub new { ... };
sub xyzzy { ... };
1;
This module is then used in several places throughout the application as:
require Foo;
my $foo = new Foo ( );
print $foo->xyzzy ( );
Now I would like to subclass Foo but still use the class name Foo. That is, I don't want to subclass Foo as, say, Bar because this would require changes to all the references to Foo throughout the system - something that is undesirable for those implementations in which I do not want to subclass Foo. (Imagine Foo as being a system module used by other system modules.)
So, to do this I moved the original Foo to a directory named System and created a new Foo in a directory named Custom as follows:
# The new Foo.
package Foo;
require System::Foo;
@ISA = qw ( System::Foo );
...
1;
And then set @INC to be:
- First, the parent directory of Custom and System.
- Second, the directory Custom.
- Third, the directory System.
With this, "require System::Foo" (needed in the subclass) will work as will "require Foo" which will find the custom Foo module if it exists and will find the system Foo module if it doesn't.
So far so good. I can get the system to at least find my custom modules before the system modules. This example doesn't work though unless I change the namespace of the system Foo by changing the package call. That is, I have to change the definition of the system module as follows:
package System::Foo;
...
So I'm willing to do this to the system objects but there's still a problem with those modules that do not have a custom subclass. That is, now all the places where I used the system module Bar as "require Bar" or "new Bar" have to be changed to "require System::Bar" and "new System::Bar." Well, that doesn't help at all as when I want to subclass Bar I would have to change these all back again.
I've tried various things like using a variable for the package name but perl doesn't like that. (I take it "package" is a compile-time command and not a run-time one.) I also tried moving the %Foo:: namespace to %System::Foo:: after loading the module but this failed because of a circular require path though I might try this route again unless I hear a better solution.
The only other solution I can think of is to write stub modules for all the system modules I might like to subclass someday. These stubs would subclass (in an identity relationship) the system modules but would reserve the namespace Foo for future changes. That is, the system modules would be named System::Foo but would only ever be accessed through the stub modules and so appear to be named straight-up Foo. Then I would change my installer to overwrite the stub modules as needed. This seems really ugly.
But then, is what I'm trying to do really ugly? Perhaps. I'm pretty sure the same problem exists in C++ and Java - maybe I'm just spoiled by the openness and flexibility of perl in so many other situations.
Thanks for any help or ideas,
Mike
Edit kudra,
2002-06-06
s/pre/code/, added readmore
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.