in reply to Re^2: Multiple Pages with CGI
in thread Multiple Pages with CGI

Some of the perl web-application frameworks do use a mapping from apache handlers to perl subs... or with some other sort of callback magic. I think maypole does that, CGI::Application also does.

If you want a request to call a sub, you just need a dispatch table keyed by page name, or id

#use strict unless untested_code; use Template; use CGI qw/header/; print header; # hash of subs that return 2 element list of page title and page conte +nt. my %title_and_content_for = ( # about_us => \&MyModule::get_about_us_html, '/quick_hack' => sub { return ( 'this is a quick, nasty hack', 'some junk to go in the page' ) }, # ... ); my %template_variables = ( # the default is to complain, this will be replaced below title => 'Oh noes!', content => q[ I couldn't find the page you asked for ], # add other stuff to keys in %template_variables ); my $html_layout = q{ <html> <head><title>[% title %]</title></head> <body> [% content %] </body> </html> }; # call the sub for this page, if there is one. # assign the results of the sub to the keys "title" and "content" @template_variables{title,content} = $title_and_content_for{ $ENV{ PATH_INFO } }->() if defined $title_and_content_for{ $ENV{ PATH_INFO } }; # and feed the whole lot to Template toolkit my $TT = Template->new() or die $TT->error(); $TT->process(\$html_layout,\%template_variables) or die $TT->error();

Now we view http://mysite.com/cgi-bin/sub-madness.cgi/quick_hack and if all goes according to plan (and my untested-make-it-up-on-the-spottery is as good as I think it is) you'll get a nice little html page that reads "some junk to go in the page".

I've cheated a little, and stuffed most of the HTML generation into places you can't see, the subs on one hand, and Template::Toolkit on the other

Other interesting this to do:

Update

@_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

Replies are listed 'Best First'.
Re^4: Multiple Pages with CGI
by gloryhack (Deacon) on Mar 26, 2007 at 16:34 UTC
    Maybe I was being a mite too pedantic? A URL is just a string. If a function/method is called, it's done by the application that handles the request and generates the response, not the URL.

    Yep, I was being too darn pedantic. 'Scuse me.

      gloryhack, you are being a pedant, but it's a good point, the uri itself won't call anything...

      The uri tells the browser to ask the webserver to run the handles that run the script which runs perl which runs my code, which runs the subs...

      But let's leave out some of those parts, and say the uri does run the subs... <phb>That's all implementation anyway</phb>

      @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
        The pedantry thing isn't in my nature, it's just a habit when dealing with the inexperienced so they stand a chance at understanding reality and don't have a cloud labeled "... and then a miracle happens..." in the equation.