Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Handling different requests for a web app in one centralized place.

by William G. Davis (Friar)
on Apr 30, 2004 at 22:45 UTC ( [id://349544]=note: print w/replies, xml ) Need Help??


in reply to Handling different requests for a web app in one centralized place.

Yes there is: Table-driven programming.

For example, have several different sub-components that each do all of the input processing and HTML generation for a single page, then build a big lookup table (hash) that stores each sub-component name, then have the appropriate sub-component called using some query string parameter you embed in each form. In this example, I use the "op" parameter to tell us what operation (page) we're going to execute, then go look it up in the table, and if it exists, we execute it:

my %sub_components = ( default_page => 'defaultpage.comp' full_text_search => 'fulltextsearch.comp', filename_search => 'filenamesearch.comp', view_alignment => 'viewalignment.comp', add_folder => 'addfolder.comp' ... ); if ($op) { if (exists $sub_components{$op}) { # execute the appropriate sub-component: eval { $m->comp($sub_components{$op}, %ARGS) }; if ($@) { # handle die() from the sub-component... } } else { # handle a request with "op" set to an invalid operation... } } ... <%args> $op => 'default_page' </%args>

Notice, you have to pass the query string arguments you received by hand to the sub-component you excute using %ARGS. (You have to do this *even* if the sub-components are all defined in the same file as the top-evel component.) Another option is caller_args(), but that's kind of hack-ish.

  • Comment on Re: Handling different requests for a web app in one centralized place.
  • Download Code

Replies are listed 'Best First'.
Slightly OT: MVC
by nmcfarl (Pilgrim) on May 01, 2004 at 02:13 UTC

    This is good plan.

    Beyond that it reminds me a good bit of building a controller, from the classic Model-View-Controller Pattern. It doesn't look much like a mason controller though because it is bypassing all the normal name and handler based dispatching. Anyhow I was wonder if anyone had adapted the normal mason handling to be more MVC like, and through the glory of google I found that someone had.

    Not that I'm suggesting anyone follow the example, merely marveling at it's existance.

Re: Re: Handling different requests for a web app in one centralized place.
by dragonchild (Archbishop) on May 02, 2004 at 02:57 UTC
    How is "table-driven programming" any different than a simple dispatch table? How is your dispatch table any different than the framework that CGI::Application provides?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      It's not my post, but I did recommend the strategy. I did so based on my belief that it basically is a dispatch table, and that you'd be rebuilding a bit of CGI::App.

      CGI::App is a good module, but I wouldn't bringing it into a mason application, particularly for use on just one page. Sometimes rolling your own is the best solution, particularly when it's something as simple as a dispatch table.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://349544]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-18 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found