In scalar context, it's usually called the flip-flop operator.

What it's doing is printing all the lines that contains *end* and all the lines containing *start*, and every line in between. If *start* or *end* occurs in the middle of a line, so be it. If multiple *start* or *end* tags exist in one line, so be it.

The whole concept of line is kinda odd when dealing with binary data anyway. Usually one reads fixed-width blocks of data when dealing with binary data because you'll never know how long a line will be. (0 bytes? 1 bytes? the entire file?)

Assuming fixed-width delimiters, your code should look like this:

#!/usr/bin/perl -w use strict; use constant BLK_SIZE => 64*1024; sub process { my ($data) = @_; print($data); # Or whatever } sub read_more { my $fh = shift; my $rv = read($fh, $_[0], BLK_SIZE, length($_[0])); die $! if !defined($rv); return $rv; } my $start = '*start*of*junk*data*'; my $end = '*end*of*junk*data*'; my $qfn = "file"; open( my $fh, '<:raw:perlio', $qfn ) or die("open $qfn: $!\n"); my $buf = ''; my $in_junk = 0; while (read_more($fh, $buf)) { if ($in_junk) { my $pos = index($buf, $start); if ($pos >= 0) { process(substr($buf, 0, $pos, '')); substr($buf, 0, length($start), ''); $in_junk = 1; redo; } else { process(substr($buf, 0, -length($start)+1, '')); } } else { my $pos = index($buf, $end); if ($pos >= 0) { substr($buf, 0, $pos+length($end), ''); $in_junk = 0; redo; } else { substr($buf, 0, -length($end)+1, ''); } } } process($buf) if !$in_junk;

In reply to Re: Range question by ikegami
in thread Range question by magawake

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.