Solved

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

Posted on 2007-11-25
4
2,166 Views
Last Modified: 2013-12-25
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
Comment
Question by:StevenMiles
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20348464
What was the assignment on or near
/home/www/MySite/root/cgi-bin/SA_TrialSignUp02.pl line 65.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 20348823
You could probably duplicate the error with an assignment like
%args = $ENV{QUERY_STRING};
0
 

Author Comment

by:StevenMiles
ID: 20349851
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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 150 total points
ID: 20351881
> 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now