Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Context aware functions - best practices?

by jmuhlich (Acolyte)
on Jan 15, 2003 at 21:27 UTC ( [id://227233]=note: print w/replies, xml ) Need Help??


in reply to Context aware functions - best practices?

The CGI.pm-style shortcut that some have mentioned is nice, and if you find it appropriate for your code that's fine. But do be aware of the consequences. Without realizing what $cgi->param() was really doing, I once wrote something like this:
%hash = ( foo => $cgi->param('foo'), bar => $cgi->param('bar'), ... );
Now, what happens when the 'foo' CGI param isn't passed? The above param() calls are in list context, so the param('foo') call returns an empty list, not an undef scalar value. Therefore $hash{foo} is set to 'bar', and any key/value pairs after that are shifted as well. wantarray is a neat tool, but you must keep your wits about you when it's afoot. :)

Replies are listed 'Best First'.
Re^2: Context aware functions - best practices?
by Aristotle (Chancellor) on Jan 15, 2003 at 22:01 UTC

    Very good point. I hadn't considered the lack of values case - what struck me immediately (the first time I was about to write such code, even) about at that snippet was the opposite case: if foo has several values you get extraneous key/value pairs - oops. So I have to agree that it's easy to create confusing semantics with wantarray.

    At this point I am confident about my choice for this particular case, and reassured about the fact I hadn't overlooked any caveats myself. Thanks and ++ to everyone for their arguments and for pointing out some implications that escaped me.

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-24 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found