Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Double value in my $ARGS hash...

Status
Not open for further replies.

rkwarner2

Programmer
Sep 3, 2009
2
US
Hello all,

I'm really new to perl so I hope I can explain what's going here well enough for someone to help me.

I working an internship where I'm developing a Perl/Mason webapp. So far it has a couple of webforms. The first, is a sort of request form where the user will have to input information about a certain type of request they are making. After the data is entered, and the form submitted, the info is processed to the DB and an email is sent. Inside the email is a link back to the form, where "THE REVIEWER" can review the request and then click a button to submit a formal review for the request. This takes them to the second, review form, where they will input the info about the review. This is where i'm having problems. I'm using the url to pass the id of the request, and a sort of boolean to the review form:

//
And I'm using the DATA:Dumper to look at what is in $ARGS:

$VAR1 = 'request_id';
$VAR2 = '82';
$VAR3 = 'new';
$VAR4 = 'true';
$VAR5 = 'review_id';
$VAR6 = '27';

And at this point everything is fine. But after I input the data on the review form and try to submit it, I can't get my if statement to pass because it's not reading the $ARGS right. The 'new' key has 2 values:

$VAR19 = 'review_date';
$VAR20 = '2009-09-03';
$VAR21 = 'new';
$VAR22 = [
'false',
'true'
];
$VAR23 = 'request_id';
$VAR24 = '82';
$VAR25 = 'request_date';
$VAR26 = '2009-08-28';

I'm guessing that the reason I'm getting the 2 values is because it's reading one the url string, and the other from my little javascript function:

//function SubmitReview()
//{
//document.getElementById('new').value = 'false';
//document.getElementById('submit').value = 'true';
//}
(i commented it out here cause i'm not sure how it render in the post. don't wanna cause any problems.)

I've tried using the $ENV hash to change the query string, removing/deleting the key before I change the value, clearing out $ARGS before the change and other thing, but so far nothing has worked.

I guess I'm looking for some ideas on how to fix this problem, or alternative methods to accomplishing what I want.

Again, I'm a newbie so keep that in mind if you reply.

Thanks.

Rk.
 
First thoughts are about your variable names.
VAR1, VAR2 ... VAR25, VAR26.
These are scary. They tell the reader nothing about their intended use. If you name your variables carefully with names that describe the intended use, that alone can often weed out bugs by making type related issues obvious.
For example, the following code appears fine:
Code:
$VAR17++;
but if the same was written like this:
Code:
$username++;
then it is clear you are attempting to treat a username as though it were an integer.

I'm not suggesting (necessarily) that you have this kind of problem but I'm simply trying to suggest a generic improvement to your coding practices that might reduce your pain in debugging any code you write.



Trojan.
 
I understand what you are saying. But I do have my variables named more descriptivily. As long as I have been using the Data::Dumper, they have always come out like that. What I was told is that the $ARGS hash holds all of the variables when the webpage is submitted and that the Data::Dumper is just a clean way to display them all. I'm using:

print "<pre>" . Dumper(%ARGS) . "</pre>";

So when,

$VAR19 = 'review_date';
$VAR20 = '2009-09-03';

prints, it's telling me that my variable name 'review_date' has the value of '2009-09-03'. This is my understanding of how the Data:Dumper works.

It you can shed more light on this, it would be welcome.

Rk.

 
Sorry, my mistake.
I guess I should have recognised the Data::Dumper variable naming convention.

Ok, the first thing I'd want to ascertain is whether the problem is client side or server side.
To do this I would suggest you display the URL that the webserver receives. Apache supplies you an environment variable called "REQUEST_URI" that you could print out to see whether the "name" parameter has multiple values on the incoming URL.
You should be able to do something like this to see it:
Code:
print $ENV{REQUEST_URI}, "\n";



Trojan.
 
If you're printing a hash with Data::Dumper, send it a reference to the hash instead. It will show it in a more logical way:

Code:
my %hash = (
  a => 1,
  b => 2,
);

print Dumper(%hash); # no
print Dumper(\%hash); # good
___

before:
$VAR1 = "a";
$VAR2 = 1;
$VAR3 = "b";
$VAR4 = 2;

after:
$VAR1 = {
  a => 1,
  b => 2,
};

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
On the multiple values thing:

if your form has two fields with the same name, it will be sent twice in the query string or POST data, and the CGI module will return it as an array.

i.e. one checkbox
Code:
<input type="checkbox" name="something" value="value">

and

Code:
my $something = $cgi->param("something"); # "value"

but two checkboxes

Code:
<input type="checkbox" name="something" value="value 1">
<input type="checkbox" name="something" value="value 2">

and

Code:
my @something = $cgi->param("something"); # ("value 1","value 2")

Note the CGI param() method, if you call it in scalar context (the first way I did) it will only return the first element, if the field was an array; in array context it returns them all. But other methods of the CGI module will return it as an array regardless, it looks like the way you're using it is ending with this result.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top