The problem you've got is that your range operator still works line by line.
E.g.
while ( <> ) {
print if m/start/i .. m/end/i;
}
Is still using the record separator as \n
- each iteration of the loop will read another line from the file, but you won't be able to match the whole chunk ... because it may not have read ahead that far yet.
You could do this via regular expression matching 'Start .. end' chunks:
#!/usr/bin/env perl
use strict;
use warnings;
my @chunks = do { local $/; <DATA> =~ m/Start.*?end/mgs };
print grep { m/MATCH/ } @chunks;
__DATA__
Start
bla
bla
bla
end
Start
bla
bla
MATCH
bla
end
Start
bla
bla
bla
end
Or as another poster notes - set $/
to 'end'. This has a slight drawback, in that it'll ignore 'Start', which means you might get additional content if you don't have the two matched properly.
You might also try:
local $/ = "end\nStart";
Which will split your data properly, but again - might not handle all the scenarios correctly.
#!/usr/bin/env perl
use strict;
use warnings;
local $/ = "end\nStart";
while ( <DATA> ) {
chomp;
print "Chunk: $_\n";
print "----\n";
print "Matches!\n" if m/MATCH/;
}
These can one-liner as:
perl -lne 'BEGIN { $/ = "end\nStart" } print if /MATCH/' file