So I'm writing a CGI script that's a form blah blah...pretty standard stuff.
I'm not using strict (yes, I know, but strict breaks the rest of the site, and I don't have the authority to choke the dude that made it that way), so I assume that's the cause of this.
Now, CGI.pm allows you to creat drop-down lists via the popup_menu() function, and specify the values with an array, and the labels of those values with a hash. The hash should have the same keys as the values in the array, and the labels you want to display should be the values of those keys.
Thus:
- Code: Select all
@array = [0, 1, 2, 3];
%hash->{0} = 'foo';
%hash->{1} = 'bar';
%hash->{2} = 'baz';
%hash->{3} = 'bang';
(yes, I know that's long form on the hash, but it's more readable)
So the menu has the values of 0, 1, 2 and 3, and shows the text of the options to be foo, bar, baz and bang.
OK...Here's where we enter the twilight zone.
I wrote subs to generate those arrays and hashes for all the drop-downs on the form, and call them thusly (names have been changed because their purpose makes them look like gibberish):
- Code: Select all
@menu1_list, %menu1_labels = makeMenu1();
@menu2_list, %menu2_labels = makeMenu2();
and so on...
And the functions return thusly:
- Code: Select all
return @menu1_list, %menu1_labels;
return @menu2_list, %menu2_labels;
and so on...
I have 6 such menus. 5 of them worked exactly as intended, displaying properly on the page and submitting the right values.
The 5th of the 6 in order of operations...well...let's say it's the retarded step-cousin or something.
The dataset it's query returns is:
- Code: Select all
ID | Last_Name | First_Name
----------------------------------
1 | Foo | Bar
2 | Baz | Bang
I pushed the value 0 into the array beforehand, and assigned the value to key 0 in the hash as "Choose One" - so there'd be a default I could validate on instead of forcing what could potentially be an incorrect value on submission.
So, the hash SHOULD look like this (again in long form for readability) once it's been fed by the sub:
- Code: Select all
%hash->{0} = 'Choose One';
%hash->{1} = 'Foo, Bar';
%hash->{2} = 'Baz, Bang';
Except, it instead looked like:
- Code: Select all
%hash->{0} = 1;
%hash->{1} = 1;
%hash->{2} = 2;
%hash->{'Choose One'} = undef;
The code for each of the 6 drop-down menus is IDENTICAL. I wrote one, it worked, and I copied and pasted it 5 times to make the other subs. The ONLY differences were variable names (all of which I checked, and even changed to see if some naming convention was hitting a builtin or something), and the field the query checks in it's where clause (irrelevant to such a problem).
Now...hopefully someone sees WHY this happened already (if so, PLEASE enlighten me as to why it behaved as it did, and only for ONE of the six subs), if not...remember this is context madness, and I'm not using strict.
So, when I assigned the variables up top when calling the sub, it was meddling with the already assigned values that the sub was generating - because I was a sillyhead and called them the same thing in both - after finding out that if they WEREN'T named the same, the variables wouldn't have values when it came time to render the HTML.
How did I fix it? Well...thanfully since it's not using strict, I don't have to worry about the values evaporating after the sub completed...
- Code: Select all
Before:
@menu1_list, %menu1_labels = makeMenu1();
After:
makeMenu1();
Works with or without return values specified in the sub.
The other ones work either way, the 5th one in the chain only works the latter way...
but....WHY?!

