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

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?
  • Comment on Re: Re: Re: (Ovid -- bug in your hand-rolled CGI code) Re: Pushing w/ an associative array?

Replies are listed 'Best First'.
(Ovid - here's another example) Re(5): Pushing w/ an associative array?
by Ovid (Cardinal) on Dec 27, 2000 at 22:27 UTC
    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.

Re: Re: Re: Re: (Ovid -- bug in your hand-rolled CGI code) Re: Pushing w/ an associative array?
by davorg (Chancellor) on Dec 27, 2000 at 22:19 UTC

    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.

        merlyn is right about what he says about security risks, but my code does does handle any form that is thrown at it. You can get the list of form parameters it has processed by using:

        keys %form
        --
        <http://www.dave.org.uk>

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

        This is already smelling of a potential BIG security hole. Can you tell us more about your application? Why would you need to be able to handle any combination of form elements?

        -- Randal L. Schwartz, Perl hacker