http://www.perlmonks.org?node_id=320256


in reply to Re: fethrow_array return value
in thread fetchrow_array return value

In the case in question, there was no need to get the actual data, the only question was if there were any rows returned.

Also, does an empty list always evaluate to false? I guess it should... I'm just confused because it appears that this evaluation is not always doing what I expect. Maybe the problem lies elsewhere....

Replies are listed 'Best First'.
Re: Re: Re: fethrow_array return value
by Roger (Parson) on Jan 10, 2004 at 01:03 UTC
    In the case in question, there was no need to get the actual data, the only question was if there were any rows returned.

    In that case you could do with a different approach -

    my $sql = "SELECT count(*) FROM table WHERE .... "; ...

    This query will return a single row containing the count of matching rows (row count) in the table. I believe this is the most efficient and portable way to determine the number of rows, and the fastest too.

      and the fastest too.

      Perhaps the most intuitive, but not necessarily the fastest (update: the fastest way to determine if there are any rows, that is, as the OP asked for). If your database is able to return rows as they're found (i.e. it doesn't have to return the entire result set like MySQL), and if there are any rows, it can be faster to do a select 1 from table... than to do a select count(*) from table....

        I meant the fastest way to determine the number of rows though.

returns an empty list, oh my (was: Re: Re: Re: fethrow_array return value)
by ysth (Canon) on Jan 11, 2004 at 06:07 UTC
    Also, does an empty list always evaluate to false?
    I am delighted at finding myself inconsistent! I feel passionately that "returns an array" is bad documentation, but "returns an empty list" doesn't make me blink twice, even though from a language-semantics point of view, a routine in scalar context cannnot return an empty list, and return () will in fact be an undef result (and hence false).

    Do any others find their brains working this way? Or would everyone else consider "returns an array" and "returns an empty list" equally good or bad?