in reply to Re: Strange behaviour - cgi related
in thread Strange behaviour - cgi related

Thanks, duff!

Just to clarify

my $in = param('place'), my $place = seewhatsthere( data => $in, field => 'Place', );
Is that correct way to pass the value from the checkbox?

Replies are listed 'Best First'.
Re: Re: Re: Strange behaviour - cgi related
by duff (Parson) on Dec 16, 2003 at 04:49 UTC

    That's a way. The key is to not use param() in a list context. You can force scalar context as you have done (by assigning to a scalar) or you can use the scalar operator like so:

    my %hash = ( data => scalar param('place'), field => 'place', );
      I see. I don't seem to have that problem when the param value is from a input field of type 'text' or type 'password'. To make my question more concrete, here's an example
      # html <tr><td>Username<td><td><input type="text" name="username" size="25">< +/td></tr> # cgi my $username = seewhatsthere( data => param('username'), field => 'Username', ); sub seewhatsthere { my %hash = @_; html_start(); print Dumper(%hash); exit(0); } # Dumped $VAR1 = 'data'; $VAR2 = ''; $VAR3 = 'field'; $VAR4 = 'Username';
      In the above case, even though the username is not filled, the dumped output is as expected. Which means that if the input type is 'text', passing param() to a list is permissible ( as shown in the code ). Am I getting it right?

        I was going to just point you at the documentation, but when I looked it didn't seem to be clearly stated. It's only when the parameter may be multivalued that you have to worry about list vs. scalar context. For single valued parameters, you always only get a scalar.

        The docs for CGI actually say ...

        Pass the param() method a single argument to fetch the value of the named parameter. If the parameter is multivalued (e.g. from multiple selections in a scrolling list), you can ask to receive an array. Otherwise the method will return a single value.

        Something more akin to "If the parameter is multivalued, you will get a list in list context and a scalar in scalar context" is more accurate.