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

I'd like to get a list of the variables that CGI.pm is passing around. I've read How do I get at the parameters in my CGI program?, CGI variables, and the CGI.pm documentation (specifically the section titled "Fetching the Parameter List As A Hash:". Unfortunately, one of my (many) shortcomings as a new programmer is that it's difficult for me to read about an abstract idea and put it into real practice. I hope as time goes on, this won't be so difficult.

I've tried the following code to print out my hash list of CGI variables. I'm expecting to see something about the cartoon/sitcom couples I've listed to show up, but I get nothing returned (other than the line "Begin testing area.")

My greater purpose in understanding this snippet is so that I can potentially use it in my program. I'm using HTML::Template, CGI.pm to generate some web pages. From one of the generated web pages, I'd like to click on one of the cells in a table and launch the next .pl file, but carry along with it some of the variables that help define what that cell is (for instance, income account row intersecting with the current month row).

Below is what I have:

#!/usr/local/bin/perl5_8 use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use Data::Dumper; my $CGI = new CGI ( { 'fred' => 'ethel', 'ricky' => 'lucy', 'fred' => 'wilma', 'barney' => 'betty' } ); $|=1; print $CGI->header; print "Begin testing area.<br>\n"; my ($params, @foo, %params); $params = $CGI->Vars; print $params->{'address'}; #<--- what is "address"? @foo = split("\0",$params->{'foo'}); %params = $CGI->Vars; print $params->{'address'}; use CGI ':cgi-lib'; $params = Vars;
I don't know what 'address' refers to, but I typed the example almost exactly as I saw it in the CGI.pm docs (I used $CGI instead of $q since that's what I have in my real program). I also often use Data::Dumper to help me see what's actually in my variables but it also shows nothing (I'm just learning D::D as well).

Any ideas on how to get a list of passed variables? Thanks!

Lori

Replies are listed 'Best First'.
Re: Getting hash of CGI variables
by matthewb (Curate) on Jan 22, 2004 at 20:29 UTC
    The section of the documentation to CGI.pm that you are looking at demonstrates the capture of both single parameters and groups of parameters with a common name. This is explained in the two paragraphs that follow the example.

    Assuming that you have passed a parameter named `address' to your script you may access it in a number of ways, commonly:
    my $value = $q->param('address');
    ...where $q is your CGI object. In the event that you want to put all the form parameters into a hash using the Vars method, you may access the value as follows:
    my $v = $q->Vars; my $value = $v->{'address'};
    In both of these examples, the only significance of the word `address' is that it was the name of the parameter passed by the referring form (or otherwise for the purpose of demonstration).

    The line with the split illustrates how, having used the Vars method, one might separate a list of values with a common parameter name into an array. This may, however, be done in the following, more natural way:
    my @array_of_values = $q->param('multi_values');
    ...where `multi_values' is, perhaps, the name of a bunch of checkboxes in the referring form.

    MB
      Thanks for the clarification and explanation. I appreciate your taking the time to explain what's going on so I can better use what I see.

      You have touched on one area that gives me the most heartburn... I don't know when examples are literal or placeholders.

      For instance, I now understand that "foo" and "bar" are what I think of as placeholders for the "real stuff" but I don't recognize things in some examples (like the word 'address') as something I need to replace with a real item. I'm hoping this is something that experience will teach me along the way. Then again, it may be the way I learn things... I learn by replicating what I see, and then tweaking the <bleep> out of it until I fully understand what the snippet does. Again, thanks!

      Lori

        I don't know when examples are literal or placeholders.

        This is slightly offtopic, but I just thought I might give some general advice. Most of the time, the things that are "placeholders" are either (1) variables or (2) strings. Exceptions should be pretty obvious, but if in doubt you can always simply play around with the example.

        One thing to be careful of, though, is when you see the same "placeholder" used more than once. If there are two instances of $foo in a snippet, you can usually change them to whatever you want, but just make sure you change them both to the same thing. Hopefully this too is obvious, but sometimes what's obvious to one is not to another. =^)

Re: Getting hash of CGI variables
by davido (Cardinal) on Jan 22, 2004 at 20:31 UTC
    You're just tripping on your shoelace a little. It's not as bad as it seems.

    $params = $CGI->Vars; puts a hash reference in $params. The next line of the CGI.pm POD gives the example of dereferencing an example 'key' contained in the hash. $params->{'address'} is only an example of how easy it is to read a param value after you've loaded it into an anonymous hash. The author could just as easily have said $params->{'example_key'}.

    Your line that says %params = $CGI->Vars; is wrong, because you're attempting to assign a hash REFERENCE to an actual named hash, rather than to a scalar (scalars hold references). You could modify that line to this:

    %params = %{$CGI->Vars};
    That dereferences the anonymous hash and assigns it to a real hash, but it doesn't really gain you anything except perhaps one level less of abstraction. And if you do that, you can no longer say $params->{'address'}. You would have to remove the dereferencing operator (->), as in "$params{'address'}".

    You probably should have a look at perlreftut, perllol, and perlref to gain a stronger working knowledge of how references work. Then it should all come together for you.


    Dave

      <sarcasm> Uh... thanks for the references to yet more reading material that is often over my head...</sarcasm>

      <great big silly grin>

      Seriously, thanks for the links. It will help me figure out where my knowledge is a bit(?) on the thin side. I've discovered that there are a lot of basics that I don't have, and have unceremoniously ignored in my drive to meet my client's deadline.

      Just in case the more experienced programmers out there read this... it truly is helpful to have links like davido's to help me (and I suspect other newbies) figure out where I/we need to get more of the basics. It's tough when you don't even know what question to ask, or where to look!     :-D

      Thanks for taking the time to help educate me!!

      Lori

        You're certanly welcome.

        I know the POD can be indimidating. I resisted the POD's until after finishing the Llama book and most of the Camel book. But in retrospect, it was a self-imposed resistance; some of them aren't really all that bad.

        In my previous post, I listed the POD's that deal with references, as it seemed that was where you were getting into trouble. I started with perlreftut, which is a tutorial on references. It seems (to me) to be the easiest one to grasp first. I recommended perllol second (discussion of "lists of lists", which is closely related also to hashes of hashes). And third I recommended perlref, which is a lot more involved, in-depth discussion of references. I probably also should have rounded it out with perldsc (Data-structure cookbook). If you take about two hours and read through each of those in approximately that order, references will soon become a familiar friend.

        A suggestion that worked well for me; each day pick a POD and read it. You'll be done in a month or so. And even though at first some will be over your head, by the time you're done you will really be able to put the pieces together, and you'll be surprised at how much of it you start to understand as you read. I need to do it all over again now that I understand most of what's there. ...thanks for reminding me. ;)


        Dave

Re: Getting hash of CGI variables
by jdtoronto (Prior) on Jan 22, 2004 at 20:28 UTC
    This will print everything:
    foreach my $name ( $CGI->param() ) { my $value = $CGI->param($name); print "The value of $name is $value<br>"; }

    $CGI->Vars; is just a simple way of impoting the whole hash ionto your main:: Namespace

    jdtoronto

      This worked very well (as did some of the other examples given in this post). Thanks!!

      Lori

Re: Getting hash of CGI variables
by Zed_Lopez (Chaplain) on Jan 22, 2004 at 20:35 UTC

    address was just an example of a possible parameter a CGI script might be looking for. The CGI docs have two examples, the second one starting use CGI ':cgi-lib';. They're not meant to be used in the same script.

    Try just:

    my $params = $CGI->Vars; print Dump($params);
      I didn't realize the POD was giving me two separate examples (could my newbieness be any more obvious.... <sigh>)...</sigh>

      Worked like a charm once I changed the "Dump" to "Dumper" (got an error message about "Dump" so I tried "Dumper" and it worked), but I now have a simple list of my parameters. Thanks!!

      Lori

        Yeah, sorry about that -- obviously my fingers were thinking about YAML, not Data::Dumper.

Re: Getting hash of CGI variables
by BUU (Prior) on Jan 23, 2004 at 07:02 UTC
    Going at it from a slightly different viewpoint, perhaps it might be simpler/easier to just use CGI::Lite which provides exactly one method to get the form data =]. You simply use the object->parse_form_data and get your hash, like so:
    use CGI::Lite; my $c = CGI::Lite->new(); my %form_data = $c->parse_form_data(); #or pass it GET or POST, respec +tively print keys %form_data; print "\$form_data{ $_ } = $form_data{$_}\n" for keys %form_data;
      Thanks for the very clear example! We don't have CGI::Lite on our system here, but I think I'll download it via ActiveState to my PC and play with it some. I might be able to sweet-talk the SA/DBA folks to consider loading it. ;-)

      Lori

        Well, it's a pure perl module so installing is as simple as copying 'Lite.pm' in to a folder named CGI someplace in your path.