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

Dear Monks,

I've the following problem:
my @a = &requestDB(..........) ; if ( defined @a) { } else { .... }
So, for example, if test requests something from a database. The request can return nothing (no match found inside the database), or something went wrong.....
How do I tell the differnce from this array ?
Should I use references ?

Thanks in advance
Luca

2006-01-21 Retitled by planetscape, as per Monastery guidelines
Original title: 'sub retruns array or empty array'

Replies are listed 'Best First'.
Re: sub returns array or empty array
by holli (Abbot) on Jan 20, 2006 at 14:27 UTC
    You should simply test for
    if ( @a )
    That will catch all cases:
    • the sub returns undef -> false
    • the sub returns an emtpy array ->false
    • the sub returns a filled array ->true
    The problem with your code is, that even an empty array is defined, so your comparison always returns true.


    holli, /regexed monk/
Re: sub returns array or empty array
by Fletch (Bishop) on Jan 20, 2006 at 14:11 UTC

    If "nothing went wrong but there were no results" is a valid case you need to distinguish then you'd probably be best off returning undef on error and an array reference if successful. Then you could use defined $results to check for error, and check for @{ $results } == 0 to see if there were no matches.

    Update: Gah, chopped out the "... is a ... distinguish ..." phrase somehow while editing. Makes a bit more sense now.

      sounds good!
Re: sub returns array or empty array
by radiantmatrix (Parson) on Jan 20, 2006 at 15:23 UTC

    Somewhat related to the comment by Fletch above, you could use an ARRAYref on success (which would be empty for no results), and a scalar containing an error message on failure.

    sub requestDB { ... if ($some_error) { return "This error occurred: $DBI::errstr"; } return \@results; # reference to results array } my $a = requestDB(...); if ( ! ref $a ) { # if $a is not a reference, it's a scalar that holds the error mes +sage die $a; } elsif (@$a) { # if @$a is nonzero, there were results ... } else { # there weren't results print "No results found, sorry!" }
    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: sub returns array or empty array
by ptum (Priest) on Jan 20, 2006 at 14:02 UTC

    If something goes wrong in my database connection or statement, and I'm using the DBI, I can check $DBI::errstr. Both $DBI::err and $DBI::errstr should be false in the case where there was no match, but the statement was OK.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde

    2006-01-21 Retitled by planetscape, as per Monastery guidelines
    Original title: 'Re: sub retruns array empty array'

Re: sub returns array or empty array
by Sioln (Sexton) on Jan 20, 2006 at 15:03 UTC
    my @a = &requestDB(..........) ; if ( defined @a) { } else { .... }
    my @a = &requestDB(..........) ; if ( scalar(@a)) { } else { .... }
Re: sub returns array or empty array
by jeanluca (Deacon) on Jan 20, 2006 at 14:14 UTC
    That makes sense, but what if you don't know what is happening inside this sub. This sub could read from file...!