carthag has asked for the wisdom of the Perl Monks concerning the following question:

I've been using CGI::Application for a while, and enjoy it because it makes coding most projects so much easier when you have a framework ready.

The problem I have is that I often end up with links that point to incredibly ugly urls, such as "http://www.site.com/script?op=do¶m1=this¶m2=and¶m3=this" or the like.

I've found a workaround that's working, and is pretty from the outside. The links now look like "http://www.site.com/script?walkfrom100to3" or "?jump40". This works fine, but is not internally pretty, as it puts a bunch of the code that really belongs in CGIApp into the index file that calls the CGIApp, like so:

use CGIApp; use CGI; ################################ my $q = CGI->new(); if ($q->query_string =~ /^keywords=walkfrom(\d+)to(\d+)$/) { $q->param(-name=>'op',-value=>'move'); $q->param(-name=>'mode',-value=>'walk'); $q->param(-name=>'location',-value=>$1); $q->param(-name=>'destination',-value=>$2); } elsif ($q->query_string =~ /^keywords=(walk|jump|run|fall)(\d+)?$/) { $q->param(-name=>'op',-value=>'move'); $q->param(-name=>'mode',-value=>$1); $q->param(-name=>'amount',-value=>$2||10); } elsif ($q->query_string =~ /^keywords=explode$/) { $q->param(-name=>'op',-value=>'explode'); } my $app = CGIApp->new(QUERY=>$q); $app->run();

Is there a way to make this prettier in the code without breaking all sorts of things?

Replies are listed 'Best First'.
Re: Pretty URLs in CGI::Application
by hardburn (Abbot) on Sep 16, 2003 at 18:42 UTC

    You could stick that code in your app under cgiapp_prerun() using prerun_mode(). See the CGI::Application documentation.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      I had tried before, but I couldn't get it to work properly, but following your advice, I looked at the documentation again and got a working solution using a sub reference in mode_param() in setup containing the code I previously had in my index file, with an added return $q->param('op') at the end. That worked! Thanks :)

      Sorry for convoluted sentence