Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

designing a dispatch class

by geektron (Curate)
on Mar 30, 2005 at 22:59 UTC ( #443628=perlquestion: print w/replies, xml ) Need Help??
geektron has asked for the wisdom of the Perl Monks concerning the following question:

I know that CGI::Application::Dispatch could do something like what I'm thinking about, but I'm not sure it's the right tool for the job.

I'm (considering) re-architecting a client application that runs under mod_perl. It's a large refactor, basically one from the ground up. There are many little and not-so-little problems with it, like blessing objects into the wrong namespaces, excessive argument and data passing, cookies set via javascript that get lost between requests .... I'll also be using more of the mod_perl hooks than vanilla CGI, etc. I'm trying to trim this thing down and rebuild for 'testability' with as little pain as possible.

I pulled out my Gang of Four book to see if there are any existing patterns to help solve the problem. I'm not 100% sure which pattern they define would be the right one, nor am I 100% convinced that I can't/ shouldn't use CGI::Application::Dispatch.

The application now isn't really 'run-modes', but separate classes that are instantiated to do specific things: a type of report, lookup table management, etc. I could translate these into run-modes at the risk of having to rewrite more. (The app currently uses $ENV{REQUESTED_PAGE} to determine the URL, look that page up in a templates table, then load the appropriate module (and template, if overridden) then perform the needed actions.)

I could feasibly build the run_modes structure needed by CGI::App::Dispatch dynamically (at least during development) ... but I'm not convinced it's the 'right' thing to do.

The task here seems simple enough: find a clean way to transform a URL into an instantiated object ... but a look through CPAN pretty much confirmed that there isn't much out there, unless it's a different name than I'm thinking.

It looks like a "Factory" pattern (from the GOF book) is the beginning of the right way to go, but are there existing modules that have already done this, or should I roll my own?

Replies are listed 'Best First'.
Re: designing a dispatch class
by tlm (Prior) on Mar 30, 2005 at 23:20 UTC

    OK, I may be way off from what you're looking for, but if you haven't yet maybe you should take a look at merlyn's CGI::Prototype. In addition to the man page for the module, see also merlyn's article and slide show.

    the lowliest monk

      looking at the article briefly, it's a little too featureless. i'll need to take a closer look at it and see if i can use it to instantiate objects ...

      ultimately, i need to do something like:

      my $foo; $foo = MyModule::ActionSet1->new() if ( condition ); $foo = MyModule::ActionSet2->new() if ( condition2 ); $foo->process(); print $foo->output();
      there are enough actions that even a hash-based dispatch table would get ugly.

      it could split into a set of CGI::Application-based classes, which is perfect for CGI::Application::Dispatch-based applications. but i'm not sure that's the direction i *should* be going.

        I found the slide show more illuminating than the article; start with that. Take a look at the activate method (there's a slide on it about 2/3 of the way through the presentation), which neatly summarizes the module's logic.

        the lowliest monk

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://443628]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2018-04-23 06:08 GMT
Find Nodes?
    Voting Booth?