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.


In reply to (Ovid - here's another example) Re(5): Pushing w/ an associative array? by Ovid
in thread Pushing w/ an associative array? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.