in reply to Downloading first X bytes of a file
Just for the sake of showing the power of LWP::UserAgent, here is a script that loads the first 512 bytes:
use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); my $url = "http://www.perlmonks.org"; my $buffer; $ua->get($url, ':content_cb' => \&first_chunk, ':read_size_hint' => 51 +2 ); print $buffer; sub first_chunk { my ($chunk, $response_ref, $protocol_ref) = @_; $buffer .= $chunk; die() if length($buffer) >= 512; }
Please note that the "size hint" is just a hint. The server may or may not follow it, so you may end up with more than you want.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Downloading first X bytes of a file
by moritz (Cardinal) on Jun 08, 2008 at 21:12 UTC | |
by CountZero (Bishop) on Jun 08, 2008 at 21:21 UTC | |
by moritz (Cardinal) on Jun 08, 2008 at 21:32 UTC | |
|
Re^2: Downloading first X bytes of a file
by pc0019 (Acolyte) on Jun 19, 2008 at 15:06 UTC |