I am trying to wean myself from CGI.pm, an easy task for most of you, but I am
a beginner. The code below works, but that does not mean it always will or that
it cannot be improved. I found the main part of this in CGI Programming with
Perl, which is 18 years old, and I’ve given up hoping that a new edition of it
will come out. I’ve read somewhere that servers can give the wrong value for
CONTENT_LENGTH, so I stuck in a second chance before bailing out; and of course
this may not be necessary or done well. I look forward to any and all
comments. I also would appreciate any guidance on how I can learn more about
CGI and how to deal with it. This book has been extremely valuable to me, but
even I can see that some of its guidance is a bit creaky! — Rick
sub get_form_data {
my $query = q{}; # Empty string
my @name_value_pairs;
if ( read( STDIN, $query, $ENV{'CONTENT_LENGTH'} )
== $ENV{CONTENT_LENGTH}
){
@name_value_pairs = split /&/, $query;
}elsif (! read( STDIN, $query, $ENV{'CONTENT_LENGTH'} ) # Second chance
== $ENV{CONTENT_LENGTH}
){
die "Trouble parsing POST request: $!"
}
foreach my $pair (@name_value_pairs){
my ($key, $value) = split /=/, $pair;
chomp ($key, $value);
$pf{$key} = $value;
}
return;
}