It really depends a lot on your template, but one thing you can do is join a few matches, the first of which will disqualify any line containing only a space (or nothing). In this example, any line that is either empty, only a new line, or any number of spaces will be rejected.
my @lines = grep { not /^\s*$/ and /$test/ } <$fp>;
Keep in mind that if the contents of the $ include test include special regexp metacharacters, they must either be designed for their metacharacters or sterilized with quotemeta() .
My theories are that you may have a string ending in \ n that somehow matches your text regular expression, or your regular $ text expression contains metacharacters in it that affect matching if you don't know. In any case, the fragment I provided will at least lead to the rejection of โempty linesโ, where empty can mean completely empty (unlikely), the end of a new line, but otherwise empty (likely), or spaces containing (possible ) lines that will be empty when printing.
source share