sub RemoveComments { my $line=$_[0]; $$line =~ s/#.*$//; # No need for a separate pattern match } .... while ($temp=){ RemoveComments(\$temp); if ($temp =~ /\S/){ ... some other code ... } } #### while ($temp=){ RemoveComments(\$temp); next if ($temp !~ /\S/); ... some other code ... }