open my $fh, '<', $filename or die "$filename: $!"; while (<$fh>) { next if /^(?:foo )?ref \d+\b/; chomp; # process the line, for example: push @lines, $_; } close $fh;