Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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,176 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

856 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