Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

  (email not shown publicly)

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Wednesday November 19, 2003
02:39 PM

Changing a CGI parameter

[ #15880 ]

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 apparently prefers ; to & in query strings. That seems weird to me; while the CGI standard needlessly defines lots of alternatives, it seems like should go with what most people use.

Another oddity is that the 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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.