use autodie; while (<>) { last if /^__END__/; next if /^\s*#/; ...; } #### my $EndOfInput = 0; for my $file (@ARGV) { if (0 == $EndOfInput) { if (open(my $fh, '<', $file)) { while (my $line = <$fh>) { if (0 == $EndOfInput) { if ($line =~ /^__END__/) { $EndOfInput = 1; } else { if ($line !~ /^\s*#/) { ...; } } } } } else { die "Can't open $file: $!"; # we are allowed to die on fatal errors } } }