in reply to Re: (Ovid -- bug in your hand-rolled CGI code) Re: Pushing w/ an associative array?
in thread Pushing w/ an associative array?

Calling param with no arguments will return a list of all of the CGI arguments. You can therefore do something like this:

use CGI qw(:standard); my %form; foreach (param) { my @args = param($_); if (@args == 1) { $form{$_} = $args[0]; } else { $form{$_} = [@args]; } }
--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

  • Comment on Re: Re: (Ovid -- bug in your hand-rolled CGI code) Re: Pushing w/ an associative array?
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: (Ovid -- bug in your hand-rolled CGI code) Re: Pushing w/ an associative array?
by Anonymous Monk on Dec 27, 2000 at 22:15 UTC
    This doesnt seem to provide the variable name, though; the only thing I'm getting is the value of the variable. Can I get both with CGI.pm?
      It does provide the variable name (I'm assuming you mean the name of a parameter passed to your script). It's $_ in the for loop. davorg's answer should work well for you. It you'd like a little demonstration of getting the name/value pairs with CGI.pm, run the following CGI script:
      #!C:\perl\bin\perl.exe -wT use strict; use CGI; my $query = CGI->new; my @names = $query->param; # Yup. That's all it takes to get all name +s # from name/value pairs. print $query->header( "text/plain" ); # This is just to show that we c +an # specify our Content-type foreach my $name ( @names ) { # If we know that a particular name only has one value, we can use # my $value = $query->param( $name ); # However, if we assign the param to a scalar and there are multip +le # values, we'll only get the first one. my @values = $query->param( $name ); print $name . "=" . (join ", ", @values) . "\n"; }
      Each $name is the "variable" and @values contains the associated values. The reason that I assigned to an array in this example is because the following query string is quite legal:
      color=blue&color=red&color=hot%20pink
      Note that the above code snippet works fine for both GET and POST methods. However, it does have one little flaw that you should be aware of: it doesn't escape potentially harmful HTML entities. That can leave your script open to annoying problems if you're just printing the values to a page (for example, if someone enters a meta refresh tag or a link to a pornographic image, you might have problems). To get around this, use URI::Escape with its associated uri_escape() function.

      Cheers,
      Ovid

      Update: Oops! Not URI::Escape! I mean HTML::Entities. Sigh.

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      I'm not sure what you mean. What variable name don't you get? The variable names are all defined in your program.

      What my snippet does is to populate a hash called %form. The keys of the hash are the CGI parameters names and the values are either the CGI parameter values or a reference to an array of values if the parameter is multi-valued.

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

        What I need to do is, since the script has to be able to handle ANY form that gets thrown at it, is take the variable names and put them in an array. That way, I can go through the array and use the param() function to get the value. When I do just a blank param() I only get the values.