in reply to If statements while in PRINT
Now with that you can write a simple function page like this to see if the variable even exists before using it.sub parse_form_data { local (*FORM_DATA) = @_; local ($request_method, $query_string, @key_value_pairs, $key_value, $key, $value); $request_method = $ENV{'REQUEST_METHOD'}; if ($request_method eq "GET") { $query_string = $ENV{'QUERY_STRING'}; } elsif ($request_method eq "POST") { read (STDIN, $query_string, $ENV{'CONTENT_LENGTH'}); } else { &return_error (500, "Server Error", "Server uses unsupported method"); } @key_value_pairs = split (/&/, $query_string); foreach $key_value (@key_value_pairs) { ($key, $value) = split (/=/, $key_value); $value =~ tr/+/ /; $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; if (defined($FORM_DATA{$key})) { $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value); } else { $FORM_DATA{$key} = $value; } } }
Don't give me credit for the code, again its paraphrased from the O'Riely CGI book, but as you can see it takes care of the problem on if you got the information or not.#!/usr/local/bin/perl &parse_form_data (*simple_form); print "Content-type: text/plain", "\n\n"; $address = $simple_form{'address'}; if ($address) { print "Your Address is ", $simple_form{'address'}, ".", "\n"; print "Please visit this Web server again!", "\n"; } else { print "You did not enter a address.", "\n"; print "But, you are welcome to visit this Web server again!", "\n" +; } exit(0);
|
|---|