Here is the current subroutine code:

#!/usr/bin/perl -w

use File::stat;
use HTTP::Headers;
use HTTP::Request;
use LWP::UserAgent;

sub upload {

 my ($file, $url, $usr, $pwd) = @_;

 if (-e $file) {
   my $fsize = stat($file)->size;

   open FH, $file;
   binmode FH;

# For dynamic file upload
my $readFunc = sub {
read FH, my $buf, 65536;
return $buf;
};
my $header = HTTP::Headers->new;
$header->content_type('application/octet-stream');
$header->content_length($fsize);
$header->authorization_basic($usr, $pwd) if ($usr && $pwd);


   # Old code: it fails with LWP 5.8
   my $req = HTTP::Request->new("PUT", $url, $header, $readFunc);

   # Patched code
   #my $ref = ($LWP::VERSION >= 5.8) ? \$readFunc : $readFunc;
   #my $req = HTTP::Request->new("PUT", $url, $header, $ref);

   my $agent = LWP::UserAgent->new;
   my $res = $agent->request($req);

   close FH;

   if ($res->is_success) {
     print $res->status_line;
   } else {
     my $msg = "Upload error: " . $res->status_line . "\n";
     print $msg;
   }
 } else {
   print "Local file not found\n";
 }
}

I have commented the patch and uncommented the old code, so it fails in my computer. I have not put any specific call example because I do not know of any public server allowing PUT requests ;-) I use it in my work intranet with a variety of Tomcat, Apache and IIS servers.

Gisle Aas wrote:

Rodrigo Ruiz <[EMAIL PROTECTED]> writes:



Yesterday I updated my LWP module from version 5.75 to the current 5.8
version. From this update, one of my scripts has stopped working.



Oops! Sorry.



The script creates a PUT request, specifying a subroutine as the
content, for dynamic content retrieval. The original code does:

     my $req = HTTP::Request->new("PUT", $url, $header, $readFunc);

But now it dies with a "Not a SCALAR reference" error.



I tried to reproduce this error but it did not happen for me. Are you able to provide a complete little program that demonstrate this failure.



I have been debugging the LWP code, and I have found the following
workaround:

     my $req = HTTP::Request->new("PUT", $url, $header, \$readFunc);

That is, pass the function reference, by reference.

Unfortunately, this change makes my script fail with older LWP versions.

My questions are:
Is there a more elegant workaround that do not break compatibility
with older LWP versions?



You could always do;

  .... $LWP::VERSION < 5.800 ? $readFunc : \$readFunc

but I rather fix this problem in 5.801.  A test case that reproduce
this would be very helpful.



If not, and I put these two lines in an if-else sentence, comparing
the $LWP::VERSION value with a "threshold" , which exact version
should I compare to?



This change went into 5.800. I'm quite sure it must be the culprit:

|    HTTP::Message will now allow an external 'content_ref'
|    to be set.  This can for instance be used to let HTTP::Request
|    objects pick up content data from some scalar variable without
|    having to copy it.

Regards,
Gisle






--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.727 / Virus Database: 482 - Release Date: 28/07/2004



Reply via email to