[Solved] perl parsing inserting new line and ^M


My guess is, that you are working with a linux file on some windows. Perl automatically converts \n into \r\n on dos-compatible machines after reading and before writing. To get rid of this behaviour, you can use binmode <FILE> on your filehandles, but it sets your filehandle into “raw binary mode”. If you want to use some other layers (like :utf8 or :encoding(utf-8)) are not enabled, and you might want to set them yourself, if you are handling character data. You also could use the PerlIO::eol module from CPAN.

Consider looking at these documentation pages:

  • PerlIO for a general understanding how the Perl-IO works.
  • open the pragma (not the function) to set layers for one program.
  • binmode the function you might want to consider.

My suggestion, but I can’t test it (no Windows around), would be to use the following:

open my $outfile, '<:encoding(utf-8)', "filename" or die "error opening: $!";
binmode $outfile, join '', grep {$_ ne ':crlf'} PerlIO::get_layers($outfile)
  or die "error setting output:\n $!"

while(<$infile>){
  s/match/replacement/g;
  print $outfile $_;
}

3

solved perl parsing inserting new line and ^M