How about this?

NB untested
use strict; use warnings; use autodie; my $filename = '...some filename...'; my $rangeStart = 10_000; my $rangeEnd = 20_000; open my $fh, "<", $filename; binmode $fh; seek($fh, $rangeStart, 0); my $total_bytes_to_read = $rangeEnd - $rangeStart + 1; my $buffer_size = 1_024; my $buffer; my $num_bytes_to_read; my $num_bytes_actually_read; # read() : Returns the number of characters actually read, 0 at end of + file, or undef if there was an error (in the latter case $! is also +set) while ( ( $num_bytes_to_read = ($buffer_size <= $total_bytes_to_re +ad) ? $buffer_size : $total_bytes_to_read ), # handle partial buffer ( $num_bytes_actually_read = read($fh, $buffer, $num_bytes +_to_read) ), ( (defined $num_bytes_actually_read) && ($num_bytes_actual +ly_read > 0) ) ) { $total_bytes_to_read -= $num_bytes_actually_read; # process $num_bytes_actually_read in $buffer } if ( ! defined $num_bytes_actually_read ) { die $! } # there was an +error. In this example, autodie will detect this, but # + without autodie you'd need to check it yourself. ( $num_bytes_actually_read == 0 ) || die "Should never happen"; # if $total_bytes_to_read > 0, then reached EOF before reading all spe +cified data # ie $rangeEnd > size of $filename

In reply to Re: Byte ranges of binary files by zork42
in thread Byte ranges of binary files by PearlsOfWisdom

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.