Re: bug in LWP::Simple::head()

Gisle Aas (aas@bergen.sn.no)
Mon, 13 May 1996 12:19:29 +0200


In message <8766.831914719@mox.perl.com>, Tom Christiansen writes:
> If you call head($url) in a scalar context on a file: URL
> 
>     if (head("file:/index.html"))  {...}
> 
> this fails, because head() only has a list return, and the last
> element of that list is the Server, which is empty, so you get a false string
> there.  This seems suboptimal.

True, and we can't guaranty that http servers will always return a
'Server' header either.  The truth is that I believed that this
construct would return the number of elements in the list (i.e. 5) in
scalar context, but now I know better.

This patch will be in the next release:

Index: Simple.pm
===================================================================
RCS file: /f/stovner/utvikling/CVSROOT/aas/perl/mods/libwww-perl/lib/LWP/Simple.pm,v
retrieving revision 1.17
diff -u -r1.17 Simple.pm
--- Simple.pm   1996/05/08 16:29:26     1.17
+++ Simple.pm   1996/05/13 10:12:42
@@ -46,7 +46,7 @@
 Get document headers. Returns the following values if successful:
 ($content_type, $document_length, $modified_time, $expires, $server)
 
-Returns 'undef' if it fails.
+Returns an empty list if it fails.
 
 =item getprint($url)
 
@@ -164,10 +164,11 @@
 {
     my($url) = @_;
 
-    my $request = new HTTP::Request 'HEAD', $url;
+    my $request = new HTTP::Request HEAD => $url;
     my $response = $ua->request($request);
 
     if ($response->is_success) {
+       return $response unless wantarray;
        return ($response->header('Content-Type'),
                $response->header('Content-Length'),
                str2time($response->header('Last-Modified')),
@@ -175,7 +176,7 @@
                $response->header('Server'),
               );
     } else {
-       return undef;
+       return wantarray ? () : '';
     }
 }
 

Regards,
Gisle.