I already linked to the docs for the Flip-Flop operator
Here an implementation
Please note how ...
- it avoids slurping the whole (potentially huge) file into RAM
- it's self documenting (well better than one big regex)
- you can now easily add more complicated tests when maintaining
use strict;
use warnings;
my $section;
my $hit;
while (<DATA>) {
my $start = /^ASDF \{\s*$/; #(2)
my $end = /^\}\s*$/;
if ($start .. $end) {
$section .= $_;
$hit = 1 if /foo_match/;
}
if ($end and $hit) {
print $section;
$section = $hit = ""; # reset (1)
}
}
__DATA__
ASDF {
tmp
foo_match
tmp
}
string2 {
tmp
}
ASDF {
tmp
bar_match
tmp
}
NB:
- 1) you can also exit instead of resetting
- 2) allowing potential "invisible" whitespace \s* at the end makes it more robust
|