CGI.pm note

Bradley Baetz bbaetz at acm.org
Sat May 3 13:37:05 UTC 2003


I discovered this evening stuff like that:

GetFormat("XXX", $cgi->param('format'), $cgi->param('ctype'));

is buggy. Thats because if |ctype| is present (and, say, set to 'Foo'),
but |format| isn't, GetFormat gets _two_ arguments ("XXX", "Foo"). this
is because $cgi->param('format') uses |return| (not |return undef|), and
that returns an empty array in array context, and so ('XXX', (), 'Foo')
becomes ('XXX', 'Foo').

This is not a CGI.pm bug (although I thought it was at first), because
of

foreach $x ($cgi->param('foo')) {
  ...
}

where an empty array is wanted.

IOW, passing $cgi->param('foo') directly into a function is only valid
if:

a) Its the last argument (and noone is checking the size of the passed
in args); or

b) The function has a prototype (For GetFormat, $;$$ would be used)
_and_ its not being called like &GetFormat which bypasses prototypes
(and its a function, not a method, because methods don't care about
prototypes); or

c) You've already checked that the param exists (in this case, though,
it should usually be stored in a scalar for speed/ease of access/etc
anyway); or

d) Its ||'d (or similar) with a default, like

GetFormat("XXX", $cgi->param('format') || 'default', $cgi->param('ctype'));

As I mentioned in (c), this is generally only important for stuff which
is accessed once, such as GetFormat (and I'll add a prototype onto that
locally, as part of my mod_perl stuff)

Bradley



More information about the developers mailing list