Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: printing in a specific area of the console

by Amoe (Friar)
on Feb 17, 2002 at 21:07 UTC ( #146042=note: print w/replies, xml ) Need Help??


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.



--
my one true love

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://146042]
help
Chatterbox?
[thepkd]: $aohoaoh ->{'foo'} has to be dereferenced to access its contents right?
[thepkd]: sorry. i mean $aohoaoh[0]
[GotToBTru]: $aohoaoh is an array reference tho
[thepkd]: some guy was suggesting $aohoaoh[0]->{'foo '}[0]->{}. This wouldnt work.
[GotToBTru]: you're mixing notation there I think
[GotToBTru]: $aohoaoh->[0] is a hash reference, so $aohoaoh->[0]->{ key1} would work
[1nickt]: If 1nickt is the some guy, that's not what he said.
[GotToBTru]: thepkd might not know about last hour of cb
[1nickt]: my $x = $aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work
[thepkd]: its not $aohoaoh->[0]. its $aohoaoh[0]->{'foo '}. Tried it. Its working. Thnaks

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2016-12-06 13:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (105 votes). Check out past polls.