Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Cool uses for multi-value GET/POST parameters

by Dallaylaen (Chaplain)
on Oct 30, 2016 at 14:48 UTC ( #1174963=perlquestion: print w/replies, xml ) Need Help??

Dallaylaen has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear esteemed monks,

I'm working on a toy web framework. One of its key features is that fetching user input, like cookies and parameters, requires regexp-based validation.

The API is as follows:

my $request = shift; my $foo = $request->param( foo => '\d+' );

This would only return a value when the WHOLE foo parameter matches regexp (one or more digits in this case), empty string* (or a sane default provided as optional third argument) otherwise.

For now, this API deliberately ignores multi-valued query parameters, only using the LAST value. The time has come to fill this gaping hole.

I'm thinking of the following method:

my $request = shift; $request->param_arrayref( bar => '\d+' );

Note that we don't rely on calling context, but require an explicit statement ("list expected!") instead.

One thing I'm confused about is what I should do if the value passed by user contains both matching and non-matching values. Filter out matching ones? Return empty array? Return undef (and possibly break someone's code in runtime)? Croak is not a variant - this is left for the user.

I'm leaning towards the second options (a missing array is MUCH easier to debug than a missing element in the array). But may be there are other concerns? Or am I doing it wrong altogether? And btw, can arrayref be shortened to just array?

Last but not least, what could be the real-life usage examples of passing the same argument multiple times in a web form? I'd rather do some trial and error as well as write a meaningful example which I can click through with my browser.

Thank you,

*To be replaced with undef in next version.

Replies are listed 'Best First'.
Re: Cool uses for multi-value GET/POST parameters
by haukex (Bishop) on Oct 30, 2016 at 19:03 UTC

    Hi Dallaylaen,

    I think that which variant is best will depend on the actual uses later on, which you won't know until you've implemented a few things using the framework. So I might design the API in such a way that it already now allows for all variants. Just a rough idea:

    $request->param_arrayref( bar => '\d+', nonmatching => 'filter' ); # or 'croak', 'undef', etc.

    That of course raises the question of a useful default. If you can't decide on one now, then you might even go so far as to have the first version of the framework force the user to include the nonmatching option on every call. Then, once you've implemented a few web apps with the framework, you can choose a useful default. Or, perhaps even better, you could allow the user to explicitly set the default on a per-webapp level, so each user is free to decide on their preferred default behavior.

    Hope this helps,
    -- Hauke D

Re: Cool uses for multi-value GET/POST parameters
by fullermd (Priest) on Oct 31, 2016 at 02:27 UTC

    Last but not least, what could be the real-life usage examples of passing the same argument multiple times in a web form?

    <select multiple>? <input type="checkbox">? Enter-one-or-more text fields, maybe.

      Thanks for pointing out. I'll try to make some examples now and see what the best usage could be.
Re: Cool uses for multi-value GET/POST parameters
by Anonymous Monk on Oct 30, 2016 at 21:31 UTC

    Hi

     param_arrayref ... And btw, can arrayref be shortened to just array?

    Where is the arrayref? And array? Sounds like jargon :D

    The latest CGI uses multi_param ... like params but longer :)

      Thanks for pointing out, I overlooked this. Of course I'm going to copy CGI's interface where applicable. Principle of least astonishment for the win.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1174963]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2020-12-04 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (58 votes). Check out past polls.

    Notices?