1

I'm trying to set a parameter to be passed to the next page via the param function.

I've tried

param(-name=>'foo',-value=>'the value');
param(-name=>'fooz',-values=>['an','array','of','values']);
param('foo3', 1);

My script redirects to another which checks for all (param), but no luck. The CGI manpage implies this should "just work". What am I doing wrong?

Thanks!

5
  • Please describe the error you are getting, if any. Commented Apr 12, 2012 at 17:37
  • @MichaelSlade The subsequent page does not receive these parameters whatsoever Commented Apr 12, 2012 at 18:16
  • +1: I didn't know Perl supported negated barewords like -name. Commented Apr 12, 2012 at 19:22
  • @AdrianPronk it doesn't, anything that appears before the => is implicitly quoted Commented Apr 13, 2012 at 14:33
  • @JD: That's not entirely true. In my Perl (5.10) $x = -name; sets $x to the string '-name' but %x = (+name => 1) creates a key with the value 'name', not '+name'. Commented Apr 14, 2012 at 4:35

2 Answers 2

4

There are a few ways to do this.

  1. Build your redirect URI with the help of the URI::QueryParam methods.

    use URI qw();
    use URI::QueryParam qw();
    
    my $q = CGI->new;
    my $u = URI->new($q->url);
    $u->query_param(foo => 'the value');
    $u->query_param_append(fooz => qw(an array of values));
    $u->query_param(foo3 => 1);
    $u->as_string   # expression returns serialised string
    
  2. Save the state of the parameters to a file and then load the state of that file from the redirected page.

    my $query = CGI->new;
    $query->save(\*FILEHANDLE);
    my $q = CGI->new(\*INFILEHANDLE);
    
  3. Do the redirect through JavaScript, though this will not let you change params.

    my $q = CGI->new;
    print $q->header();
    print "window.location=\"$url\";\n\n"
    

In my opinion, the first option I have listed above is the easiest to do.

Sign up to request clarification or add additional context in comments.

5 Comments

I thought parameters could be passed regardless of whether the object was used or not. I'm using my $query = CGI->new; $query->param(-name=>'foo',-value=>'the value'); print $query->redirect( -URL => 'cookietest.cgi' ); with no results. cookietest.cgi has read params with and without the object with same empty results
@JD It sounds like you're trying to set params on a different request (via redirect). Setting the params on the object will override the params on your current request, but it won't help you with the new request via your redirect. Try print $query->redirect(-URL => 'cookietest.cgi?foo=bar') to pass params to a new request.
@JD I updated my example with steps for redirecting with params. One of those options is what oalders said.
Brant's answer, together with oalders' comment, are spot on. If you need to pass on the key/value pairs to the next page that you're redirecting to, you need to actually pass them on. Alas, CGI doesn't have Session support built in like you do with Java Servlets and the like, you actually have to do the work yourself. Brant's 2nd option is essentially doing this, but I agree with him in that you should probably just settle for his first option.
Those are great suggestions. I am wondering if there is a way to mimic the form action submission way of parameter passing. Can you do this in Perl CGI?
1

You need to use the self_url method.

$cgi->param(-name=>'foo',-value=>'the value');
$cgi->param(-name=>'fooz',-values=>['an','array','of','values']);
$cgi->param('foo3', 1);
printf '<a href="%s">next page</a>', $cgi->self_url;

This becomes f.ex. <a href="http://localhost:5000/foo.pl?foo=the%20value;fooz=an;fooz=array;fooz=of;fooz=values;foo3=1">next page</a>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.