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:

+-modules/
  |
  +-Base/
  | |
  | +-MainApp.pm
  | |
  | +-FooBar.pm
  |
  +-Client1/
  | |
  | +-MainApp.pm
  | |
  | +-FooBar.pm
  |
  +-Client2/
    |
    +-MainApp.pm
    |
    +-FooBar.pm

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 startup.pl 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 startup.pl 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 FooBar.pm 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,
meraxes

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


In reply to Namespace repair - better way to do it? by meraxes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.