Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How do I save the result of POST to a file?

by ChrisR (Hermit)
on Jul 30, 2003 at 15:10 UTC ( [id://279206]=note: print w/replies, xml ) Need Help??


in reply to How do I save the result of POST to a file?

I agree that an upgrade of Perl would be the first step. However, in some instances, upgrades and loading of modules may not be allowed. In that case, I have a couple of routines that may help out. The first will simply get the form parameters into a string which you can then manipulate yourself:
sub GetParams { my $webparams; if ($ENV{'QUERY_STRING'} ne '') { $webparams = "$ENV{'QUERY_STRING'}"; } else { read(STDIN, $webparams, $ENV{'CONTENT_LENGTH'}); } return $webparams; }
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:
$hash{input_name}[0] $hash{input_name}[1]
To find out how many items were returned for a given key, use:
my $numitems = $#{$hash{input_name}};
This routine takes one or two parameters which are specified in the code:
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; }
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 DecodeURL { my $text = shift; $text =~ tr/\+/ /; $text =~ s/%([a-f0-9][a-f0-9])/chr(hex($1))/eig; return $text; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://279206]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found