Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Easy way to run tests on many CGI params?

by ncw (Friar)
on Aug 31, 2000 at 01:25 UTC ( [id://30412]=note: print w/replies, xml ) Need Help??


in reply to Easy way to run tests on many CGI params?

The if statement in check_all is wrong - you are using the comma operator in the expression - probably not what you intended. This will return true provided $param{benefits} is set. If you replace the ,s with || then I expect it will work. (Update: or even && as geektron suggests below :-)

You can put all the parameters into a hash by using the param() method of the CGI module in list context. This returns you all the parameter names. Eg something like

my %hash = map { $_ => $cgi->param($_) } $cgi->param();

Replies are listed 'Best First'.
RE: Re: Easy way to run tests on many CGI params?
by merlyn (Sage) on Aug 31, 2000 at 03:10 UTC
    But, don't do this in general, because multi-value params lose the ability to pull up the multivalues. You've just regressed into what cgi-lib.pl got oh-so-wrong, and one of the things that CGI.pm fixes properly.

    -- Randal L. Schwartz, Perl hacker

      If you were worried about this you could collect the parameters like this :-
      my $hash = {}; for ($cgi->param()) { push @{$hash->{$_}}, $cgi->param($_); }
      However I find that 9 times out of 10 you want the simpler hash, so you can collect the data twice in a slightly more complicated way something like this :-
      my $hash = {}; for ($cgi->param()) { push @{$hash->{list}->{$_}}, $cgi->param($_); $hash->{scalar}->{$_} = $hash->{list}->{$_}->[0]; }
      You can then access most data with a simple $hash->{scalar}->{'whatever'} but for those cases when you cared about multiple entries you can do $hash->{list}->{'whatevers'}->1

      I think the CGI module is fantastic, but I do find it much more convenient to have my data in a hash rather than have to call it from param() when I need it. For a CGI of any length then I'd certainly use one of another of the methods above.

        If you want your data easily broken out so you don't have to call param, the easiest way is probably just to use the import_names call:
        IMPORTING ALL PARAMETERS INTO A NAMESPACE: $query->import_names('R'); This creates a series of variables in the 'R' namespace. For example, $R::foo, @R:foo. For keyword lists, a vari- able @R::keywords will appear. If no namespace is given, this method will assume 'Q'. WARNING: don't import any- thing into 'main'; this is a major security risk!!!!
        Then you still get both the single item $R::foo and the occasional list item @R::bar. And no code to cargo-cult-schlep around.

        -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found