in reply to printing in a specific area of the console
A nice way to do this is the way provided by the LWP::UserAgent module. It allows you to provide a code reference that gets invoked whenever it gets another chunk of a file. Check this:
my $sub = sub { my ($data, $res, $proto) = @_; # either write directly to file or push onto an array $filename ? print FILE $data : push(@res_lines, $data) +; # increment total length $data_recved += length $data; if ($already_drew_percentage) { # erase an old sign and draw a new one print "\b" x 9, sprintf('%03d%% done', (($data_recved / $res->content_length) * 100) +); } else { # draw the first percentage printf('%03d%% done', (($data_recved / $res->content_length) * 100) +); $already_drew_percentage = 1; } };
(Sorry about the tab mess...I'm not sure how to correct it.) Call it like this:
my $res = $agent->request($req, $sub, 1024)Where $agent is an LWP::UserAgent, $req is an HTTP::Request and 1024 is the amount of bytes you want to get before invoking $sub.
That was ripped from the pronbot source. It allows you to either write binaries directly to file via the FILE handle and the value of $filename, or push the lines of the response onto an array which can later be joined. (You might have do declare some of the vars yourself :) I'd advocate using an LWP module for basic HTTP stuff, it's very easy, although this probably looks complex.
|
---|