Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2224
  • Last Modified:

Why do I get Odd number of elements in hash error and garbled input in POST to perl script

Hi,

I'm working on a perl program that will take input from various text box form fields and do some processing.  I was parsing the POST information myself, but was advised, on this site and in other reading, to use CGI.pm to do it.  In fact, I ran into a problem that if the user put "%" or "&" in a text box, it would garble the input, a problem that CGI.pm corrected.

However, now I've been seeing a new problem that I can't debug.  Here is how I am now obtaining the info from the text boxes and putting it into the hash that I use throughout the rest of the program:

use CGI;
my $query = CGI->new();
my @names = $query->param;
foreach my $name ( @names ) {
      $value = $query->param( $name );
      $args{$name} = $value;
}


Sometimes, inconsistently, I'll get the following error:

Odd number of elements in hash assignment at /home/www/MySite/root/cgi-bin/SA_TrialSignUp02.pl line 65.

Then, I have some debugging code that says:
foreach $key (keys (%args))
      {print STDERR "$key = $args{$key}\n"}

And instead of printing out the expected:

txtEmailAddress = bill@here.com
txtFirstName = Bill
txtLastName = Smith
etc.

... it prints:

txtEmailAddress=bill@here.com&txtFirstName=Bill&txtLastName=Smith =

See the lone equals sign on the right?  It looks like there was only one "key" and no "value", thus an odd number of elements.
This wouldn't be in part because sometimes a text box is left blank by the user, would it?  I have to assume that a user would leave a box blank sometimes.

I say the problem was inconsistent, in that sometimes it wouldn't happen, and the input would parse correctly, with everything else being the same, even restarting the server just before each test.
I'm using linux/Apache2/mod_perl.

After doing more reading on CGI.pm, I changed the code that processes the input, to:

use CGI;
my $query = CGI->new();
%args = $query->Vars;

which also seems to work fine, but I don't know if sometime in the future it will show up with the "odd number of elements" error, too.

I *need* the POSTs to process accurately every time.  Can anyone recognize something I'm doing wrong, or know of something different that I should be doing that would be safer and more effective?  I'd appreciate any help.

Thanks,
--Steve D.

Update: I'm having trouble re-creating the "odd number of elements" error.  Making me crazy here.  I hate problems that I can't consistently recreate.
/SD


0
StevenMiles
Asked:
StevenMiles
  • 2
2 Solutions
 
ozoCommented:
What was the assignment on or near
/home/www/MySite/root/cgi-bin/SA_TrialSignUp02.pl line 65.
0
 
ozoCommented:
You could probably duplicate the error with an assignment like
%args = $ENV{QUERY_STRING};
0
 
StevenMilesAuthor Commented:
Hi, ozo,
I guess I wasn't completely clear.  After the CGI code at the top of my post tried to assign the form fields to %arg, the "odd numbered" error occurred when I tried to call a subroutine, passing %arg to the sub.  The error happened in the subroutine, on the line: "my %DataForThisSub = @_;"

But the "foreach $key" debug code, which showed that the fields were parsed incorrectly, was immediately after the CGI code to parse the input and create %args.  The question is still why the parsing didn't work, and whether there is a safer way to parse the POST input that would avoid whatever error is happening here.
/S
0
 
ahoffmannCommented:
> It looks like there was only one "key" and no "value"
you have to check your form in HTML, it sounds that it sends a variable (named "", means empty or just a space) without a value.
Keep in mind that the key=value pair in the search string of an URL is no (RFC)standard, nearly anything you can imagine is legal here.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now