Re: DBI: selectrow_array() return first column when called in list context

Tim Bunce <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.dbi.general
Message-ID <[email protected]>
On Mon, Dec 23, 2013 at 09:30:22AM +0000, [email protected] wrote:
>    The SQL query is the same in both cases, and as can be seen, the query is expected to return two columns
>    (user and perm), but selectrow_array() returns undef for the second column (perm in this case).
> 
>    my ($x, $y) = $dbh->selectrow_array(qq{SELECT user,perm FROM access
>    WHERE token=\'Tt9VpStL4xADSDJQtd4AkM
>    c6cVi66Mwmr9pMcCRgO4NVJ\'})|| die
>    "Could not query database: $DBI::errstr\n";

Your code could be summarized as:

    my ($x, $y) = foo() || die "...";

Note that the "||" operator forces the foo() call to be evaluated in
scalar context, so only a single value is returned. It's evaluated
something like this:

    my ($x, $y) = ( foo() || die "..." );

If you used the "or" operator instead you'd get the expected results
because the or operator precidence is below that of the "=" operator
so it's evaluated like this:

    (my ($x, $y) = foo() ) or die "...";

Tim.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.