use Algorithm::Loops qw( Filter ); my @lines = grep defined, Filter { s/^\s+//; if( /^#/ ) { undef $_; } else { s/\s+$//; } } <$in>;