in reply to How do I save the result of POST to a file?
This one is a bit more complex. It will return a hash where the keys are the names of the inputs on the html form and the values are, of course, the values of the inputs. If you have things like multi-select list boxes, you should choose to return a hash of arrays. The data will then be accessed like:sub GetParams { my $webparams; if ($ENV{'QUERY_STRING'} ne '') { $webparams = "$ENV{'QUERY_STRING'}"; } else { read(STDIN, $webparams, $ENV{'CONTENT_LENGTH'}); } return $webparams; }
To find out how many items were returned for a given key, use:$hash{input_name}[0] $hash{input_name}[1]
One important thing to remember is that the data sent from the html form may be encoded. I have another very simple routine that is quite helpful in decoding this data:sub GetParamsHash { my $type = shift; # type = 0 or not included returns a hash # type = 1 returns a hash of arrays my $webparams = shift; my $webparams2; my %webparams3; if(!$webparams) { if ($ENV{'QUERY_STRING'} ne '') { $webparams = "$ENV{'QUERY_STRING'}"; } else { read(STDIN, $webparams, $ENV{'CONTENT_LENGTH'}); } } if($type == 1) { $webparams2 = $webparams; $webparams2 =~ s/\&/\n/g; $_ = $webparams2; while(/^(.*?)=(.*)$/gm){ push @{$webparams3{$1}}, $2; } } else { $webparams2 = $webparams; $webparams2 =~ s/\&/\n/g; $_ = $webparams2; %webparams3 = /^(.*?)=(.*)$/gm; } return %webparams3; }
sub DecodeURL { my $text = shift; $text =~ tr/\+/ /; $text =~ s/%([a-f0-9][a-f0-9])/chr(hex($1))/eig; return $text; }
|
|---|