Menu

#10 LWP::Protocol::collect take IO objects

open
nobody
None
5
2001-05-30
2001-05-30
No

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

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.