Subject: | results() error handling, Null::Empty, Null::Error |
The WWW::Search man page says:
Show quoted text
> results
>
> Return all the results of a query as an array of WWW::Search::Result
> objects.
> ...
> On error, results() will return undef and set response() to the HTTP
> response code.
Accordingly I wrote some code like:
my @results = $search->results();
if (!defined @results) {
my $Response = $search->response;
if ($Response->is_error) {
die $Response->error_as_HTML,"\n";
} else {
die "WWW::Search::$search_engine: returned undefined result set (a bug)\n";
}
}
if (!@results) {
die "no matches found\n";
}
and tested it out with the 'Null::Empty' back-end. Unfortunately it always triggers the second die. That's because you can't distinguish between an undefined and an empty array in Perl.
On the other hand, if your code is supposed to be returning a scalar undef or (undef), such that $results[0] == undef, then the documentation should be clarified, and code like this:
my @results = $search->results();
if (!@results) {
die "no matches found\n";
}
if (!defined $results[0]) {
my $Response = $search->response;
if ($Response->is_error) {
die $Response->error_as_HTML,"\n";
} else {
die "WWW::Search::$search_engine: returned undefined result set (a bug)\n";
}
}
should work. But running the above code against 'Null::Error' produces the same 'no matches found' response as 'Null::Empty' produces. It seems that whether there is an error or not, @results is an empty array.
Something isn't quite right in either the implementation of Null::Error, Null::Empty (or the WWW::Search methods that process their results), or the documentation.
-Tom