[dancer-users] Retrieving params of fields with multiple values
Aaron Crane
perl at aaroncrane.co.uk
Fri Aug 22 13:25:30 BST 2014
Yanick Champoux <yanick at babyl.dyndns.org> wrote:
> On 14-08-22 06:25 AM, Andrew Beverley wrote:
>> However, I still think it would be a neat little feature to be able to
>> write:
>>
>> my @foo = param 'foo'
>>
>> (a bit like you can with uploads). Would a patch for such a feature be
>> accepted?
>
> At first glance, it's a good idea. I would just have to
> double-check that it doesn't break anything critical. Like, I'm thinking
> that code that does
>
> my @params_i_want = map { param($_) } qw/ foo bar baz /;
>
> would suddenly behave differently.
I think a change of this sort would be a fairly bad idea, to be
honest. There's lots of reasonable code that would suddenly break if
param() changed from returning a single value in list context to
returning a possibly-empty list. You've given one example here;
another is (say):
my $searcher = SearchEngine->new(
query => param('q'),
lang => param('lang'),
);
I discussed these issues (including context-sensitivity in
specifically a param()-like routine) at some length in a talk at
YAPC::EU a couple of years ago:
http://aaroncrane.co.uk/talks/calamitous_context/
That said, I do understand why someone would want to be able to get a
list of all the values for a given parameter name. Instead of changing
the predictable behaviour of param(), I'd suggest adding a new routine
that does precisely what you want, perhaps along these lines:
sub param_list {
my ($name) = @_;
croak "param_list() must be called in list context"
if !wantarray;
my $value = param($name) // [];
return ref $value eq 'ARRAY' ? @$value : $value;
}
I think it might also be worth changing param() to return an empty
array ref (instead of the undefined value) when there's no parameter
of the desired name, but I'm less sure about that.
--
Aaron Crane ** http://aaroncrane.co.uk/
More information about the dancer-users
mailing list