Skip Menu |

This queue is for tickets about the WWW-Search CPAN distribution.

Report information
The Basics
Id: 13029
Status: resolved
Worked: 10 min
Priority: 0/
Queue: WWW-Search

People
Owner: MTHURN [...] cpan.org
Requestors: tmetro [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 2.476
Fixed in: (no value)



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
It seems to me that all would be fixed if results() returned an empty list rather than undef. Do you agree? (I have made the changes and I can upload a new distro any time) - - Martin 'Kingpin' Thurn
From: tmetro [...] cpan.org
[MTHURN - Thu Jul 7 23:17:27 2005]: Show quoted text
> It seems to me that all would be fixed if results() returned an > empty list rather than undef. Do you agree?
No. (If I remember correctly.) As I previously stated, "you can't distinguish between an undefined and an empty array in Perl." I think your options for indicating an error condition are: 1. return a list consisting only of undef, such that @results is true (because there is one element), but $results[0] == undef. 2. change the API to return an array reference. Then you can simply return an undef on error. For backwards compatibility, you could use wantarray() to to return a list to old code. 3. communicate the error condition in some other way, such as throwing an exception via Carp. Probably #2 is best. -Tom