[Catalyst] Re: retrieving multiple values from forms

A. Pagaltzis pagaltzis at gmx.de
Mon Dec 17 23:27:46 GMT 2007


* Andrew Rodland <arodland at comcast.net> [2007-12-17 22:40]:
> On Monday 17 December 2007 03:01:53 pm Matt S Trout wrote:
> > On Sat, Dec 15, 2007 at 01:10:52PM -0600, Andrew Rodland wrote:
> > > See the perldoc for Catalyst::Request -- the 'param' method
> > > comes in handy here.
> > >
> > > @values = $c->req->param('whatever');
> >
> > No it doesn't.
> >
> > That method is there for CGI.pm compatibility. Don't use it
> > in new code.
> 
> It's also the only one that has a sensible semantic. "Might be
> an unblessed scalar or an arrayref" is silly for something
> that's naturally a _list_. If param wasn't there I would have
> to write a method that did the same thing, and use that.

None of these interfaces is sane. The problem with CGI.pm’s
interface is this:

    Foo->new(
        bar => $q->param( 'bar' ),
        baz => $q->param( 'baz' ),
    );

Now if someone sends you this query string:

    ?bar=1;baz=2;baz=is_admin;baz=1

the call evaluates to


    Foo->new(
        bar => 1,
        baz => 2, 'is_admin', 1,
    );

Woops.

So CGI.pm forces you to pepper your code with `scalar`s to make
it safe:

    Foo->new(
        bar => scalar $q->param( 'bar' ),
        baz => scalar $q->param( 'baz' ),
    );

Catalyst’s interface avoids that. However, Catalyst’s way of
“either you get an arrayref or a scalar” isn’t sane either since
it forces you to pepper your code just as much if not more, if
you want it to react predictably regardless of how many arguments
were passed.

The *sane* thing is neither.

The sane thing is to have *TWO* methods, one that *always*
returns a scalar, and one that *always* returns a list. (Or
rather, an arrayref, because if it’s just a list, it is easy to
improperly treat it like a scalar, whereas if it’s an arrayref
it’s impossible to forget to unpack the array when you meant to
do that.)

Then you can say what you mean, you can say it *easily* either
way, and the result is always completely determined by what the
code says and not at all by what the data looks like. Sanity.

-- 
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>



More information about the Catalyst mailing list