my $interesting_bit; { # maybe open here . . . local $_; while () { if ($_ eq "--start foo--\n" .. $_ eq "--end foo--\n" ) { $interesting_bit .= $_; } elsif ( $interesting_bit ) { last } } # . . . and close }