se strict; use warnings; #why negate, reverse the operations. Also, specifying STDIN is more self documenting. my $infile = @ARGV ? shift(@ARGV) : ; my $outfile = @ARGV ? shift(@ARGV) : ; handlefix($infile,$outfile); open(INFILE, '<', $infile) or die "\nCan't open $infile: $!\n"; open(OUTFILE, '>', $outfile) or die "\nCan't open $outfile: $!\n"; print OUTFILE map { my $s=$_; $s=~s/\s*#.*$//; $s } (grep { !/^\s*#/ } ), "\n" ; #close INFILE && close OUTFILE; close returns a value... if the first close fails Perl will ignore second close close INFILE; close OUTFILE; sub handlefix { for(@_){ chomp($_); $_=~s/"//g; $_=~s/\//\\/g; } }