LWP::Protocol::collect should accept IO refs (e.g., IO::Handle and children, file handles, etc.) for $arg, not just file names and coderefs.
This would support code like this:
use IO::File;
use LWP::UserRequest;
my $tmpio = IO::File->new_tmpfile;
my $response = (new LWP::UserRequest)->request
(new HTTP::Request (GET => $theURL),
$tmpio);
# Do something clever with $tmpio.
As things stand, I need to do this instead:
my $tmpio = IO::File->new_tmpfile;
my $response = (new LWP::UserRequest)->request
(new HTTP::Request (GET => $theURL),
sub { print $tmpio $_[0] });
I much prefer the former to the latter, for reasons of clarity, maintainability, and generality. Basically, and non-coderef that supports a print method should work for $arg. I have HTML::Parser::parse_file in mind as an example of this kind of flexibility.
A sample implementation for LWP::Protocol::collect looks like, inserting after the coderef handling section:
elsif (ref($arg) and $arg->can('print')) {
# read into callback
while ($content = &$collector, length $$content) {
if ($parser) {
$parser->parse($$content) or undef($parser);
}
LWP::Debug::debug("read " . length($$content) . " bytes");
# Use eval here in case method dies
eval {
$arg->print ($$content);
};
if ($@) {
chomp($@);
$response->header('X-Died' => $@);
last;
}
}
}
Cheers,
--binkley