CGI
's param()
function behaves differently if called in list or scalar context. In scalar context, the first (and most the only) parameter value is returned. In list context, all parameter values are returned (remember that CGI allows to have multiple values per key).
If you call your function like this, then list context is used. This means that $cgi->param('field1')
evaluates to the empty list, and the first value of $cgi->param('field2')
is assigned to $a
in the subroutine. To avoid this, you have to explicitly force scalar context. This can be done by using scalar
:
foo(scalar($cgi->param('field1')), scalar($cgi->param('field2')));
Another possibility is to use intermediate variables:
my $field1 = $cgi->param('field1'); # scalar context here
my $field2 = $cgi->param('field2');
foo($field1, $field2);