list context of cgi param problem in hash initialization

D

david

Hi all,

I have a script with the following

%a = (foo=>$cgi->param('foo'), boo=>$cgi->param('boo'))

I have a problem if foo was not defined then the cgi thinks that i
want list context and returns an empty array.
then the order of the hash is broken and for $a{foo} i get boo.

one way is to write
%a = (foo=>scalar $cgi->param('foo'), boo=>scalar $cgi->param('boo'))

but this is not elegant.

Has someone a more elegant solution for this problem ?

Thanks in advance,
David
 
G

Gunnar Hjalmarsson

david said:
I have a script with the following

%a = (foo=>$cgi->param('foo'), boo=>$cgi->param('boo'))

I have a problem if foo was not defined then the cgi thinks that i
want list context and returns an empty array.
then the order of the hash is broken and for $a{foo} i get boo.

one way is to write
%a = (foo=>scalar $cgi->param('foo'), boo=>scalar $cgi->param('boo'))

but this is not elegant.

Has someone a more elegant solution for this problem ?

Other way (but not more elegant IMO):

%a = (
foo => $cgi->param('foo') || undef,
boo => $cgi->param('boo') || undef,
);

or maybe:

%a = $cgi->Vars;

But in the latter case, the key 'foo' does not exist. Only you can tell
whether that matters.
 
T

Tim McDaniel

I have a script with the following

%a = (foo=>$cgi->param('foo'), boo=>$cgi->param('boo'))

I have a problem if foo was not defined then the cgi thinks that i
want list context and returns an empty array.
then the order of the hash is broken and for $a{foo} i get boo.

one way is to write
%a = (foo=>scalar $cgi->param('foo'), boo=>scalar $cgi->param('boo'))

but this is not elegant.

Has someone a more elegant solution for this problem ?

Um, am I missing something about why

$a{foo} = $cgi->param('foo');
$a{boo} = $cgi->param('boo');

doesn't work? Or, if the cgi param and the hash index are always
required to be equal, and if you have a list of many of them,

$a{$_} = $cgi->param($_) for qw(boo coo goo loo moo poo roux too woo zoo);

?
 
J

Jim Gibson

david said:
Hi all,

I have a script with the following

%a = (foo=>$cgi->param('foo'), boo=>$cgi->param('boo'))

I have a problem if foo was not defined then the cgi thinks that i
want list context and returns an empty array.
then the order of the hash is broken and for $a{foo} i get boo.

one way is to write
%a = (foo=>scalar $cgi->param('foo'), boo=>scalar $cgi->param('boo'))

but this is not elegant.

Has someone a more elegant solution for this problem ?

If you want the input parameters as a hash, you can call the Vars()
method, which returns a reference to a hash. You could then use a hash
slice to copy the parameters you want to another hash, if needed.
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top