http://www.perlmonks.org?node_id=237637


in reply to LWP: Downloading First 2KB of an HTML File

Of course, a Range header will fail in a lot of cases, where the webserver doesn't know ahead of time the size of the document it is going to transmit.

In that case you can use the $ua->request($request, \&callback, 4096); form of the request method. LWP::UserAgent will then call your callback function as it downloads, passing it chunks of the specified length.

Now the POD to LWP::UserAgent says this:

The request can be aborted by calling die in the callback routine. The die message will be available as the "X-Died" special response header field.

Obviously, the easiest route in this case is to request chunks of 2kb, and unconditionally die whenever the callback is called. That will do exactly what you want: load 2kb, then abort the request.

my $data; $ua->request($request, sub { $data = shift; die }, 2048);

There you go.

Makeshifts last the longest.