Changing a CGI parameter

jdavidb on 2003-11-19T19:39:44

Had to think a little while to figure out the right way to produce a URL from a CGI program pointing to the same program with a parameter added/changed/overridden. I started out appending the parameter, then realized instead I needed to instead change it if it already existed in the middle, and wondered for a bit what the "right way" to do this was (i.e., most robust and using a module).

It's taking me a little while to get back into the swing of things with CGI, and I'm surprised. It's been about a year since I wrote a CGI program on the job.

sub changeurl
{
  my($cgi, %newparams) = @_:
  my $newcgi = CGI->new($cgi);
  foreach my $param (keys %newparams)
  {
    $newcgi->param($param, $newparams{$param});
  }
  return $newcgi->url(-relative => 1, query => 1);
}

One thing I consider an oddity is that CGI.pm apparently prefers ; to & in query strings. That seems weird to me; while the CGI standard needlessly defines lots of alternatives, it seems like CGI.pm should go with what most people use.

Another oddity is that the CGI.pm documentation says you can get the "unprocessed query string" with the query_string method, but you're obviously getting a processed and regenerated query string because it will replace your ampersands with semicolons.


Semicolons in the query string

runrig on 2003-11-19T22:37:44

it seems like CGI.pm should go with what most people use.

There are good reasons for making semicolon the default.

Re:Semicolons in the query string

jdavidb on 2003-11-21T16:39:18

Thank you! I knew that if I arrogantly stated it was a bad idea that would draw out someone who would point me to the reason it was not. :)