http://qs1969.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        

Replies are listed 'Best First'.
Re^5: Stop Using Perl (CGI::param)
by LanX (Saint) on Jan 03, 2015 at 22:13 UTC
    >
    sub get_param { return get_params( @_ )->[-1]; }

    If this is a feature request for CGI I'd like to see a setting to raise an exception if multiple values are received where only one value is legally expected.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re^5: Stop Using Perl (context sensitivity is a bad practice to avoid)
by Anonymous Monk on Jan 04, 2015 at 01:44 UTC

    Yeah, the problem is CGI::param() not DBI

    Hmm, did you ever submit a bug report for that? :)

    Speaking of this talk:

    bugzilla doesn't even use CGI.pm, it uses Bugzilla::CGI ... if they went that far they could have fixed the API

    movable type wasn't doing any argument validation while generating dynamic sql ... can't blame it on CGI

    Is CGI.pm's actual interface the best? No, but its been that way for the past 20+ years and its using a common perl feature (context sensitivity) ...

    but yeah, context sensitivity is just mental overhead that is easy to avoid