http://qs321.pair.com?node_id=1112071


in reply to Re^3: Stop Using Perl
in thread Stop Using Perl

Yeah, the problem is CGI::param() not DBI. I recall many times having to jump through extra hoops because I realized that param() might return more than one value (and I suspect there were times I didn't because I didn't). CGI::param()'s interface isn't "just" a trap for introducing bugs, it is also inconvenient. It is a bad interface, though the badness is somewhat subtle (which actually makes the problem worse, including not just fixing the interface long ago).

The general pattern is that it is fine for something that normally returns several things to, in a scalar context, return just the most interesting aspect of those things. It is a mistake to take something that normally returns just a scalar and create cases where it might return other than just a scalar. And that applies even if the function is documented as "returns all of the values for foo" but a very common case will be that you only expect or want one value "for foo".

Having the name be plural would have made the need to jump through hoops for the common case of just wanting one value more likely to be done. But it still would have been a bad interface.

A better interface would be more like:

sub get_params { .... return wantarray ? @vals : \@vals; } sub get_param { return get_params( @_ )->[-1]; }

But this is far from some fundamental problem with Perl. It is a bad practice to avoid, like can be found when you work with any language to enough depth. I find Perl actually has far fewer of these (and mostly less severe ones) than C or C++, for example.

- tye