is :read_size_hint the implementation of the HTTP ranges you are talking about?

It is one implementation of it but requires careful use of the callback. As you can see from your code, it downloads all the content but in chunks of your specified size. Since the object here is rather only to download the minumum amount of data from the server, the callback must die to stop the subsequent chunks being retrieved. eg:

#!/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'; # M +acBeth 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_hi +nt' => $bytes); print "Retrieved $linecount lines in $chunkcount chunks from $url:\n\n +$firstndata\n";

If you run this, you will see that it retrieves slightly more than the 30 lines required, but substantially less than the full text. This seems like a reasonable compromise and is, of course, tunable by the user to the specific task at hand by varying $wantlines and $bytes.


In reply to Re^7: Split file, first 30 lines only (HTTP Ranges and :read_size_hint) by hippo
in thread Split file, first 30 lines only by wrkrbeee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.