CGI.pm and param

G

garhone

I'm trying to view the contents of my CGI object, directly and using
param.
I encounter a problem with scrolling lists.

Here's my scrolling list: 'slist' => ['foo', 'bar', 'fu', 'foobar',
'fubar'];

When I use Data::Dumper on $q, the CGI object, I'll see
....
,'slist' => ['foo', 'bar', 'fu', 'foobar', 'fubar'],
....

If I try to access 'slist' directly, I can see all of it:
print Dumper $q->{'slist'};

I get
$VAR1 = 'foo'
$VAR2 = 'bar'
$VAR3 = 'fu'
$VAR4 = 'foobar'
$VAR5 = 'fubar'

But if I try
my @a = $q->param('slist');
print Dumper @a;

I get
$VAR1 = 'foo'

and that's it.

Can anyone tell me why this is the case? How can I get
$q->param('slist') to return all the contents of 'slist' instead of
just the first element?

Thanks in advance
 
P

Paul Lalli

garhone said:
I'm trying to view the contents of my CGI object, directly and using
param.
I encounter a problem with scrolling lists.

Here's my scrolling list: 'slist' => ['foo', 'bar', 'fu', 'foobar',
'fubar'];

That is not a scrolling list. That is a key/value pair, where the key
is the string 'slist', and the value is a reference to an anonymous
array. You are asking us to take your word for the fact that you are
using this key/value pair to correctly populate a scrolling list widget
of an HTML form. Please don't do that. Post a SHORT but COMPLETE
script that demonstrates your error.
When I use Data::Dumper on $q, the CGI object, I'll see
...
,'slist' => ['foo', 'bar', 'fu', 'foobar', 'fubar'],
...

If I try to access 'slist' directly, I can see all of it:
print Dumper $q->{'slist'};

I get
$VAR1 = 'foo'
$VAR2 = 'bar'
$VAR3 = 'fu'
$VAR4 = 'foobar'
$VAR5 = 'fubar'

I find that vaguely unlikely, as $q->{'slist'} is a single item, a
scalar value. So it's rather not possible for Data::Dumper to show you
five different values. Please post real code and real output. Maybe
you meant you did something like
my @values = @{$q->{'slist'}};
print Dumper(@values);
But if I try
my @a = $q->param('slist');
print Dumper @a;

I get
$VAR1 = 'foo'

and that's it.

Can anyone tell me why this is the case?

Because param() is working the way it was intended.
How can I get
$q->param('slist') to return all the contents of 'slist' instead of
just the first element?

It's not returning the first element. It's returning all values of
your scrolling list that were selected by the user filling out your
form. That is what param() returns. That is what it is intended to
return.

If you want to get a list of all possible options for this scrolling
list, you've already seen how to do it - access the relevant hash entry
in the CGI.pm object.

If you can tell us *why* you think you want param() to return something
other than what it was intended to return, perhaps we can show you a
better way.

Paul Lalli
 
U

usenet

garhone said:
Can anyone tell me why this is the case? How can I get
$q->param('slist') to return all the contents of 'slist' instead of
just the first element?

It's been a long time, but I think it's something like this:

foreach (param('slist')) {
...
}

If you had posted a short but complete program to reproduce your
question (per the group's posting guidelines) then I could have tested
and debugged the solution. Since you didn't do that, though, I'm not
able to be as helpful as I would like.
 
P

Paul Lalli

It's been a long time, but I think it's something like this:

foreach (param('slist')) {

There is no difference between using param() in a list context in a
foreach loop and using param() in a list context in an array
assignment.

Paul Lalli
 
G

garhone

First, thanks David and Paul for your help.

Second, I sincerely apologize for not posting any real code. Under
normal circumstances, I would most certainly post my code. I should
have mentioned this in the first place, that I am posting on behalf of
someone else, who showed me the problem and I have no access to the
actual code.
Also, my posting was probably further blundered by my lack of knowledge
of CGI.

I appreciate your help and I will pass on your replies, as well as the
advice to post the problem himself.

Once again, thanks.
 
G

garhone

First, thanks David and Paul for your help.

Second, I sincerely apologize for not posting any real code. Under
normal circumstances, I would most certainly post my code. I should
have mentioned this in the first place, that I am posting on behalf of
someone else, who showed me the problem and I have no access to the
actual code.
Also, my posting was probably further blundered by my lack of knowledge
of CGI.

I appreciate your help and I will pass on your replies, as well as the
advice to post the problem himself.

Once again, thanks.
 
G

garhone

First, thanks David and Paul for your help.

Second, I sincerely apologize for not posting any real code. Under
normal circumstances, I would most certainly post my code. I should
have mentioned this in the first place, that I am posting on behalf of
someone else, who showed me the problem and I have no access to the
actual code.
Also, my posting was probably further blundered by my lack of knowledge
of CGI.

I appreciate your help and I will pass on your replies, as well as the
advice to post the problem himself.

Once again, thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top