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

I have a script (not mine) which I'm trying to add a couple of things to. I have always used CGI.pm to extract parameters, but in this script environment variables are used:
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; } else { $INPUT{$name} = $value; } }
The information currently picked up from the form is:
if ($INPUT{'onething'} eq "onething") { &onething; } elsif ($INPUT{'onething'} eq "twothing") {&twothing; } elsif ($INPUT{'threething'}) {threething; } else {&formula; } exit;
I tried to get my checkboxes to function using:
if ($INPUT{'fourthing'} eq "four") { $address = "four.txt"; } elsif ($INPUT{'fourthing'} eq "five") { $address = "five.txt"; } elsif ($INPUT{'fourthing'} eq "six") { $address = "six.txt"; } else {&formula; }
But alas $address remains undefined.

I would be grateful if someone would point me in the right direction. (I could just rewrite it for CGI.pm but I would miss an opportunity of learning some more about environment vars - hence the question. :))

Replies are listed 'Best First'.
Re: Extracting name value pairs from Environment Variables
by Roger (Parson) on Sep 26, 2003 at 00:26 UTC
    Have you checked what's in the INPUT hash table?

    Try to add Data::Dumper to see what's held in INPUT.
    use Data::Dumper; ... print "Content-Type: text/html\n\n"; print "<HTML><BODY><Pre>\n" print Dumper(\%INPUT); # insert this after the foreach loop print "</Pre></BODY></HTML>\n" exit(0); ...
    And see what's held in INPUT. The reason why $address remain undefined is because the hash keys you are looking for might not exist in the hash table.

      You are absolutely right, and I thank you for showing me the use of Data::Dumper - I have now added that to my armoury...

      Once I'd seen that the values were not held in INPUT I looked more closely at the embedded HTML code and found that I had made a mistake in that. When I fixed it $address magically appeared. Thanks once again for your help.

Re: Extracting name value pairs from Environment Variables
by sauoq (Abbot) on Sep 26, 2003 at 01:58 UTC
    I could just rewrite it for CGI.pm but I would miss an opportunity of learning some more about environment vars - hence the question.

    That code isn't doing much with envariables. It simply retrieves the CONTENT_LENGTH and then uses it to know how much data it should read from stdin. During a POST request, the CGI parameters are sent to the script via stdin. The rest of that code just parses those parameters into another format. (Though, not necessarily into the easiest one to work with as I'll point out below.)

    Does your code work if you have just one checkbox checked?

    The reason I ask is that, if you have two checkboxes that both have the same name, and both are checked, then your $INPUT{checkbox_name} will contain the values separated by commas. For example, if your HTML looks like this:

    <input type="checkbox" name="c" value="foo"> <input type="checkbox" name="c" value="bar">
    then $INPUT{c} will contain "foo,bar" which, of course, will not eq either "foo" or "bar".

    In any case, it's impossible to say for sure what is wrong without more information.

    -sauoq
    "My two cents aren't worth a dime.";
    

      Thanks for bringing up that aspect. Though not part of the problem (see my response to Roger) it is a very valid point. The reason I use checkbox is to allow more than one file to be used, and when that is the case there will be a mechanism to merge the files selected.

      Thanks once again for your help.

Re: Extracting name value pairs from Environment Variables
by jonadab (Parson) on Sep 26, 2003 at 03:25 UTC
    if ($INPUT{'fourthing'} eq "four") { $address = "four.txt"; } elsif ($INPUT{'fourthing'} eq "five") { $address = "five.txt"; } elsif ($INPUT{'fourthing'} eq "six") { $address = "six.txt"; } else {&formula; }
    But alas $address remains undefined.

    Age-old debugging technique I learned in BASIC: use print statements. In particular, have your script print out the value of $INPUT{fourthing} someplace (perhaps inside <!-- -->) and maybe that will clue you in why it isn't eq to any of your criteria.


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/