[dancer-users] Retrieving params of fields with multiple values

James Baer jamesfbaer at gmail.com
Thu Aug 21 19:54:52 BST 2014


Similar result as Maxwell suggested, but here is what I do:

my $foo      = params->{'foo'};
if(defined $foo) {
                $foo = [ $foo ] unless ref $foo;
}





On Thu, Aug 21, 2014 at 2:30 PM, Maxwell Carey <mcarey at ucar.edu> wrote:

> You can coerce everything into an arrayref like this (requires 5.10+):
>
>     my $foo = param 'foo';
>     $foo = [ $foo // () ] if ref $foo ne 'ARRAY';
>     for (@$foo) { ... }
>
> $foo is now an arrayref with 0, 1, or more elements. You're still checking
> for undef and arrayref, so this is just a convenient shortcut.
>
>
> On Thu, Aug 21, 2014 at 3:55 AM, Andrew Beverley <andy at andybev.com> wrote:
>
>> Hi guys,
>>
>> I may be missing something here, so I wanted to run this by you:
>>
>> When a form is submitted that has multiple fields with the same name
>> (such as several checkboxes), the values of that field are all returned
>> in an array ref.
>>
>> However, if the same form is submitted, but only one value is returned
>> (eg. only one checkbox has been ticked) then a scalar is returned. If no
>> checkboxes are ticked, then undef is returned.
>>
>> This makes it difficult to process a form with several checkboxes, as
>> the return value from param('checkbox') can be an array ref, scalar or
>> undef, depending how many checkboxes are ticked.
>>
>> At the moment, I'm testing for each of those 3 conditions. Is there a
>> better way?
>>
>> It would be useful if param() could be called in array context, in which
>> case it would return 0, 1 or several items, depending on which ones were
>> checked.
>>
>> Thanks,
>>
>> Andy
>>
>>
>> _______________________________________________
>> dancer-users mailing list
>> dancer-users at dancer.pm
>> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>>
>
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20140821/7e0e251b/attachment.html>


More information about the dancer-users mailing list