Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Unexpected CGI param behavior

by ikegami (Pope)
on Jun 24, 2008 at 20:39 UTC ( #693825=note: print w/ replies, xml ) Need Help??


in reply to Re: Unexpected CGI param behavior
in thread Unexpected CGI param behavior

You have it backwards, as far as I'm concerned. Your snippet would still fail without wantarray. Without wantarray, my $scalar = $cgi->param('foo'); would *also* fail.

That's the case for every function that returns a list. Perl can't return a list in scalar context, so param must necessarily return something different in scalar context. wantarray simply helps return a meaningful value instead of junk.

Since param returns a list, feel free to always call param in list context if you want to avoid the issue.

my @values = $cgi->param('foo'); my ($first_value) = $cgi->param('foo'); my $first_value = ( $cgi->param('foo') )[0];


Comment on Re^2: Unexpected CGI param behavior
Select or Download Code
Re^3: Unexpected CGI param behavior
by cLive ;-) (Parson) on Jun 25, 2008 at 06:25 UTC

    "would *also* fail" - um, no it wouldn't, because it explicitly returns a scalar undef. I've tested it.

    Of course, what it would do is change an array assignment:

    # from @array=(); # to: @array=(undef);

    Which would be an unacceptable change to existing behavior (exists $array[0] value would change from empty string to 1).

      Not that situation, the situation where the field is present.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://693825]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-08-31 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls