Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: What should this do?

by chipmunk (Parson)
on Dec 05, 2001 at 09:21 UTC ( #129530=note: print w/replies, xml ) Need Help??


in reply to What should this do?

It's the code. If there are no values for a given parameter, then CGI->param() will return an empty list in list context. This is more or less necessary, because CGI has no way of knowing which parameters are expected to have a single value and which a list of values.

Unfortunately, this is a frequent gotcha in exactly the situation you show. (It's gotten me at least once!) The hash would also be messed up if the parameter returned more than one value. The solution is to call param() in scalar context, e.g.:

my %hash = ( 'key1' => 'value1', 'key2' => scalar $r->param('doesnt_exist'), 'key3' => 'value3', );
or to create an anonymous array, if appropriate:
my %hash = ( 'key1' => 'value1', 'key2' => [ $r->param('multi_valued') ], 'key3' => 'value3', );

Replies are listed 'Best First'.
(tye)Re: What should this do?
by tye (Sage) on Dec 05, 2001 at 10:18 UTC

    This is one of the reasons that I wish => imposed scalar context upon both its left and right sides. Unfortunately, rampant overuse of => as a "cute" comma in a wide variety of situations that are inconsistant with its genesis of denoting a hash's "key => value" relationship have made such an improvement pretty much impossible in Perl 5.

    I can't recall enough about Perl 6 at the moment to guess at whether this has been considered for Perl 6, would "fit in" for Perl 6, or would just no longer be an issue there. (:

            - tye (but my friends call me "Tye")
      In Perl 6, => is a constructor for the built-in PAIR datatype (see: Apocalypse 3 and Exegesis 3).

      Both arguments of the pair constructor are evaluated in scalar contexts. In the problem code above, that would cause the param method to be called in scalar context, which would cause its "empty return" to return undef instead of that pesky empty list.

      So I guess the short answer is that it...

      would just no longer be an issue there.
        There is only one real reply to this... sweet!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2020-01-18 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?