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

When using perl in cgi, I know that to get values passed with POST content, you use the <STDIN> function thing. How do you get the values from forms with GET specified as the form action? Would it be the ARGV command? Whether it is or not, could someone tell me how to get these values?

Replies are listed 'Best First'.
Re: Passing Arguments With Perl/Cgi
by chromatic (Archbishop) on Mar 19, 2000 at 00:25 UTC
    Use CGI -- the standard module for CGI programming. Getting a parameter is as easy as: my $value = $cgi->param('value');
Re: Passing Arguments With Perl/Cgi
by Anonymous Monk on Mar 19, 2000 at 01:04 UTC
    Never mind! I did a lot of searching (A LOT!) and found the answer for myself. After sitting in a chat room for nearly an hour, I got my answer:
    @pairs = split(/&/, $ENV{'QUERY_STRING'}); foreach $pair (@pairs) { local($name, $value) = split(/=/, $pair); #That code that changes + to spaces and all the other characters... (w +hich I know) }
    THERE: now I can simply get a value by calling this code: $TheVariable = $name{'TheVariableFromTheForm'}; I Hope this might help anyone else who has this same question...
Re: Passing Arguments With Perl/Cgi
by justinNEE (Monk) on Mar 19, 2000 at 08:03 UTC
    No No No, listen to chromatic and the others. :) use CGI; It may be uncomfortable using modules, but it IS neccesary for two reasons: 1> no need to reinvent the wheel EVERYDAY for the rest of your life, and 2> you are not at a point where you will right BETTER code than in the modules. One of the most attractive features of the Perl universe is that there are always smarter people out there willing to "hook you up". (= but since I know you won't listen to what I just typed, this will be useful:
    $stringNEE=~s/%(..)/pack("c",hex($1))/ge; $stringNEE=~s/+/ /g;
      while it may be necessary and uncomfortable to use other peoples code, i do belive that it is a good idea to know what the code you are using is doing. When I first used modules I was completely unfamiliar with what I was doing with them, I was just using regurgitations of what other people had done. when I realised what the area of the module that I used a lot did, I became more (ahem)enlightened(sorry.) to what the uses of perl were. so, by questioning the module and asking/researching what it was doing in certain stages I could develop a greater understanding of the language. BTW, I am a fairly extensive module user, but I still sit down and research what one will be doing if I use it a lot.
      That last one should be: $stringNEE =~ tr/+/ /; (It's a LOT faster to use tr/// than s///g.)
Re: Passing Arguments With Perl/Cgi
by turnstep (Parson) on Mar 24, 2000 at 23:46 UTC

    (It's very important to know how to do it yourself.)

    Here's my way to do it. Note how it catches invalid pairs.

    if ($ENV{'REQUEST_METHOD'} eq "GET") { for (split(/\&/, $ENV{'QUERY_STRING'}) { if (($name, $value) = /(.*)=(.*)/) { ## Normal $value =~ ## Unescape $value here } else { ## Abnormal, no "equal"! $name =$_; $value=0; } $name =~ ## unescape $name here }

    Update:Yes, I know, you should just use CGI. The above code is merely for educational purposes, please use CGI for any "real" code. :)

      Presuming your code isn't on a home personal enviroment in a lot of cases it simply isn't practical to use modules. In the case of code used on a 1000 machines say.

        If you have 1000 machines at your disposal, you also have a mechanism for file distribution among them. So that's a non issue. Yes, even you can use CPAN.

Re: Passing Arguments With Perl/Cgi
by Anonymous Monk on Mar 18, 2000 at 22:46 UTC
    Post = STDIN Command Get = ????
Re: Passing Arguments With Perl/Cgi
by Anonymous Monk on Mar 19, 2000 at 00:48 UTC
    Thanx, but what I want is the actual code to do it myself, not a module that does it for me, so any more help is welcome!
      Then look at the CGI.pm source. That's probably the best example you'll get of parsing CGI parameters, etc.

      What's your aversion to using a module? If it's because you want to learn how to do it yourself, that's great-- but if you're just reinventing the wheel for the sake of doing so, you should rethink.