in reply to /s option modifier in regex substitution

Why do you need to read the whole file into memory?
open my $FH, '<', $file or die $!; while (<$FH>) { last if /^__END__\s/; # Update: maybe /\b__END__\b/ or /(?:^|\s)__ +END__(?:\s|$)/) print; }
Also note that both the solutions incorrectly treat code like
print " __END__ ";