#!/usr/bin/env perl use strict; use warnings; use utf8; use strict; use warnings; use LWP::UserAgent; # Modify these three variables only to suit my $url = 'http://www.gutenberg.org/ebooks/1533.txt.utf-8'; # MacBeth my $wantlines = 30; # Retrieve this number of lines my $bytes = 256; # Chunk size to download my $firstndata; my $linecount = 0; my $chunkcount = 0; sub add_chunk { my ($chunk, $res, $proto) = @_; $firstndata .= $chunk; $linecount += () = $chunk =~ /\n/g; $chunkcount++; die if $linecount >= $wantlines; } my $ua = LWP::UserAgent->new; my $res = $ua->get ($url, ':content_cb' => \&add_chunk, ':read_size_hint' => $bytes); print "Retrieved $linecount lines in $chunkcount chunks from $url:\n\n$firstndata\n";