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

i'm coding under mod_perl2 / libaprq, but that shouldn't matter too much...

I often pull post info from a form as such:
$this->{'ApacheRequest'}->param( "checkbox_field");
unfortunately, that tends to return a scalar if there is 1 checkbox_field item, or an array if there are more.

I want to standardize info to act as an array, even its its a scalar (ie, create a 1 item list)
my @tmp = $this->{'ApacheRequest'}->param( "checkbox_field");
I discovered 'wantarray', but i know that i'm using it wrong
my @tmp = wantarray : $this->{'ApacheRequest'}->param( "checkbox_field +") : @{$this->{'ApacheRequest'}->param( "checkbox_field")};
Can someone enlighten me please?

Replies are listed 'Best First'.
Re: Forcing a returned array (wantarray?)
by chromatic (Archbishop) on Feb 20, 2005 at 17:56 UTC
    I want to standardize info to act as an array, even its its a scalar (ie, create a 1 item list)

    Does your code not do that? I can't think of any place in Perl where assigning a scalar to an array doesn't create a one-element array.

    Please post a short test case demonstrating that it doesn't work in this case.

Re: Forcing a returned array (wantarray?)
by jdalbec (Deacon) on Feb 20, 2005 at 18:11 UTC
    Is it returning an array or an array reference as your last code snippet seems to indicate? You can use ref to determine whether a scalar is a reference and if so what kind.
    my $ref = $this->{'ApacheRequest'}->param( "checkbox_field"); my @tmp = ref($ref)?@$ref:$ref;
    wantarray is actually intended to be used in the called subroutine (param in this case) so it can tell whether you're calling it in scalar context or array context.
Re: Forcing a returned array (wantarray?)
by esskar (Deacon) on Feb 21, 2005 at 01:05 UTC
    the idea of wantarray is a little bit different.
    e.g. a subroutine is using it to determ if the caller wants an array or a scalar
    sub foo { return wantarray ? @_ : "@_"; } my @bar = foo(1, 2, 3); # @bar = (1, 2, 3); my $bar = foo(1, 2, 3); # $bar = "1 2 3"
    but
    my @tmp = $this->{'ApacheRequest'}->param( "checkbox_field");
    should do what you want
Re: Forcing a returned array (wantarray?)
by nmerriweather (Friar) on Feb 20, 2005 at 18:12 UTC
    sometimes the simplest explanation is the best. severe user error. thank you.