Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Namespace repair - better way to do it?

by meraxes (Friar)
on Feb 27, 2008 at 16:55 UTC ( #670687=perlquestion: print w/ replies, xml ) Need Help??
meraxes has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks,

I've basically got a request concerning structure and what I'm tasked to do. I've got something... I just don't know if it's a good idea. So let me break it down to a kind of base case.

What I've got

I've got a CGI::Application-based app that we run several instances of under mod_perl. Each instance of the app gets it's own apache instance. Why not run more than one instance of the app under one Apache with mod_perl? Namespace.

Let say I have a CGI::App class for the app, Base::MainApp, and an OO module called Base::FooBar. The app doesn't run directly off of Base::MainApp but rather off subclasses of it. In this case lets say these subclasses are in folders like so:

  | |
  | |
  | |
  | |

So we have Base::Mainapp and Base::FooBar. But here's the kicker. Client1::MainApp is a subclass of Base::MainApp but Client1::MainApp is really just the package MainApp; ... and the same goes for MainApp in the Client2 folder. You see, all the files for each instance of the App do the following:

use lib 'modules'; use lib 'modules/Client1';

Obviously, if this was to be an instance of Client2, the would be changed accordingly. So, MainApp occupies the same namespace in Client1 as it does in Client2 which means I can't load both at the same time into the same apache instance under mod_perl.

What I've got to do

"Meraxes," you say, "obviously you have to put each of the subclasses into it's own namespace. Simple. Just make them Client1::MainApp and Client2::MainApp. Easy as pie!"

"But of course!" I declare. "But there is a teensy problem."

The app is structured this way because modules in each of the Client folders are like this:

package MainApp; use strict; use warnings; use base 'Base::MainApp'; 1;

This is so methods in Base::MainApp can be overridden to allow for custom behaviour in the app on a client by client basis. This is done extensively. Obviously, there's a lot more classes but this is how it works now.

The kicker

The problem lies in Base::MainApp. In Base::MainApp you'll find use FooBar and $foobar = FooBar->new(). If we're running using Client1, this loads the in the Client1 folder that is a subclass of Base::Foobar with any Client1 customizations. If I change Foobar to be Client1::FooBar, I can't use it!

What I'm doing... but is this a good way? Is there a better way?

So the namespace of the current client is in an environment variable; let's call it APP_NAMESPACE. Instead of a use statement inside Base::MainApp I'm using UNIVERSAL::require in the runmode that needs to access Client1::FooBar, so it's something like this:

use UNIVERSAL::require; sub runmode { my $self = shift; $modulename = $ENV{APP_NAMESPACE}. '::FooBar'; $modulename->require or croak $@; $foobar = $modulename->new(); # carry on... use $foobar as needed }

I've made up a little testy thing with all these modules and all seems to work just as I expected but haven't started implementing this at large in the App. The intent is to do this with as low an impact to the codebase as possible but really I'd like to know if what I'm doing is a colossal boondoggle or not. Any alternate suggestions? Are there gotcha's I'm introducing with this? Are there gotcha's I don't know about concerning UNIVERSAL::require?

Many thanks,

Update: Made a few grammatical fixes. I still let my mind run ahead of my typing.

Comment on Namespace repair - better way to do it?
Select or Download Code
Re: Namespace repair - better way to do it?
by pajout (Curate) on Feb 28, 2008 at 00:01 UTC
    Just alternate idea, please, excuse my typos, I write it directly to browser form.

    Concerning just

    use lib 'modules';
    , you can have
    package Base::MainApp; use Base::FooBar; sub new { my ($class, $prototype) = @_; unless (ref $prototype eq 'HASH') { $prototype = {foobar=>Base::FooBar->new()}; } my $self = bless $prototype, $class; return $self; } sub method_using_foobar { my ($self) = @_; $$self{foobar}->foobarize(); } #rest of module
    package Client1::MainApp; use base 'Base::MainApp'; use Client1::FooBar; sub new { my ($class, $prototype) = @_; unless (ref $prototype eq 'HASH') { $prototype = {foobar=>Client1::FooBar->new()}; } my $self = Base::MainApp::new($class,$prototype); return $self; } #rest of module
    But it very depends how the current code is organized...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://670687]
Approved by Fletch
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-09-19 05:22 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (129 votes), past polls