Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Multiple CGI parameters

by sstevens (Scribe)
on Aug 15, 2008 at 14:38 UTC ( [id://704550]=perlquestion: print w/replies, xml ) Need Help??

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

We all know that to get some parameters from CGI, you do:
use CGI; my $cgi = new CGI(); my $param1 = $cgi->param('param1'); my $param2 = $cgi->param('param2'); ..
When I have a lot of parameters, copy-pasting the $cgi-param('..') over and over gets annoying quickly. Back before I put use strict; in my scripts (about three days ago), I would do the following to overcome this:
@params = ('param1', 'param2', ..); for $param (@params) { $$param = $cgi->param($param); }
However, now that I'm using strict, I can't do this anymore. I get an error that I can't declare a scalar dereference. I see that you can do things like:
my @names = $cgi->param;
or
$cgi->import_names('Q');
They just aren't as convenient as the other method. With the other method, I could get all the variables and then do things like
print "bla bla $param1 bla\n";
But now I would have to do something like
print "bla bla " . $Q::param1 . " bla\n";
I know I'm complaining about something that really doesn't matter too much. I'm just wondering what you monks do when you retrieve a large set of parameters. Although there are a plethora of other threads that deal with getting parameters with CGI.pm, I couldn't find any that answer this question. My apologies if such a thread already exists.

Thanks!

Replies are listed 'Best First'.
Re: Multiple CGI parameters
by injunjoel (Priest) on Aug 15, 2008 at 18:57 UTC
    Greetings,
    For some reason my first attempt at posting didn't work out
    No matter... I would suggest looking at the Vars() method/function in CGI, which will give you all your parameters in a nice neat hash. There is also example code to handle multi-valued parameters.

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
        (mainly, it concatenates duplicate variables, using the "\0" character)


        I just ran into that issue and spent a few days trying to figure out what I thought was a UTF-8 encoding issue. So much fun.
Re: Multiple CGI parameters
by jhourcle (Prior) on Aug 15, 2008 at 16:09 UTC

    The great thing about 'use strict' is that when you know that it's being a problem, you can turn it off:

    for $param (@params) { no strict 'refs'; $$param = $cgi->param($param); }

    however, in your case, as everything's a scalar, I'd probably use the 'Vars' function to just dump it all to a hash: (or hashref, in this particular example)

    use CGI ':cgi-lib'; $params = Vars;

    Update: chromatic is completely correct. You shouldn't turn off strict unless you're sure it's a good idea, as it can get you into trouble. If you're going to use the above trick, @params should come from a list that you define, and not from a call to params() or anything that might be tainted.

      Disabling strict in this way is a very bad idea. PHP used to do this, but after several years of PHP applications being broken into by malicious users automatically overwriting global variables by passing in carefully-crafted parameters, even PHP disabled this feature.

      You should not recommend that anyone do this.

      no strict 'refs';
      Now that is cool! Thanks!

      Update:
      Okay, okay, it isn't cool. I'm still just using the repetitive version.
        It's not cool. It's not big and it's not clever.

        You need to have a very good reason to turn off strict or warnings.

        This wasn't a good reason


        Unless I state otherwise, all my code runs with strict and warnings
Re: Multiple CGI parameters
by Lawliet (Curate) on Aug 15, 2008 at 15:43 UTC

    I think I am misunderstanding the problem here. Why is print "bla bla $params[0] bla\n"; not as convenient as print "bla bla $param1 bla\n"; ?

    I'm so adjective, I verb nouns!

    chomp; # nom nom nom

      You're right, it's just as convenient. The only problem is it would be hard to keep track of which parameters are where if accessed via $params[0], $params[1], etc. This is assuming that the parameters aren't actually param1, param2, .., but something like id, pw, ... If they were just param1, param2, .., then that would work just fine!
Re: Multiple CGI parameters
by Anonymous Monk on Aug 15, 2008 at 14:45 UTC
    use a hash

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://704550]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found