--- William McKee <[EMAIL PROTECTED]> wrote:
> On 21 Jun 2001, at 9:50, Curtis Poe wrote:
> > > foreach ($q->param) {
> > >     $REQUEST{"$_"}=$q->param("$_");
> > > }
> > 
> > I assume that you know your form better than I do, but are you aware that
> > the above snippet will only return the first value for a param if that
> > param has several values? For many forms, this will not make a difference.
> 
> I am working on a script which has just this problem. My solution was 
> to look for the name of the field, get an array and assign the array as 
> the value in the hash. Besides knowing which fields may contain 
> multiple values, is there a way to determine the number of values 
> returned by $q->param(foo)?
> 
> Thanks,
> William


Um, yes, but it looks ugly.  Generally I only use the following method if I have to 
generate a
form on the fly.  Here's one way of handling something like this:

    my $foo_count = scalar @{[param('foo')]};

This will tell you how many values have been returned for foo.  Another way:

    my @foo      = param('foo');
    my$foo_count = scalar @foo;

What's going on is that param() uses 'wantarray' to determine if it's assigning to a 
scalar or
array.  If it's assigning to a scalar, it only returns the first value (or only value, 
if that's
all there is).  Otherwise, it's going to return a list.  What the first method does it 
the
following:

    [ param('foo') ]

The square brackets force param() to be evaluated in a list context.

    @{[param('foo')]}

The @{...} forces the listref to be dereferenced into an anonymous array.

    scalar @{[param('foo')]};

That scalar function then forces the anonymous array to be evaluated in scalar 
context, thus
returning the number of elements.

If only one form element is returned for 'foo', param will just return a one element 
list and the
above construct returns the value '1'.

Cheers,
Curtis Poe

=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Reply via email to