in reply to Perl line endings: something broken in ActiveState Perl 5.8?


It may help you to know that this doesn't work with previous versions of ActivePerl either. So I guess that you are mis-remembering the previous behaviour. ;-)

The problem is that there is no \r in the line that perl sees since it is stripped off by the Windows IO libraries. Therefore you can't remove it. And when the line is written back out the \n is replaced with \r\n (again by the IO libraries) so the \r is still in the output file.

You can convince yourself of this with the following one-liner which won't print out any of the lines in file.

perl -ne "print if /\r/" file.txt
The following will work when directed to another file:
perl -pe "BEGIN{binmode *STDOUT}" file.txt > file2.txt

This won't work with -i however, probably due to where the binmode occurs in relation to other internal actions on the input and back-up file.

--
John.

Replies are listed 'Best First'.
Re^2: Perl line endings: something broken in ActiveState Perl 5.8?
by rduke15 (Beadle) on Oct 13, 2005 at 08:50 UTC
    Amazing! Yes, you are right. Searching on my hard drive, I also found ActivePerl 5.003_07, the non-Activestate GS Perl 5.004_02 , and even the DOS port 5.003_93. None of them seem to work, even binmode'ing everything.

    perl -i.bak -ne "BEGIN{binmode STDIN; binmode STDOUT}; s/\x0D//;"

    It is true that I mostly used this in Linux, but I'm still surprised that I would never have noticed after all these years. Has this never been reported as a bug?
      You can get Perl to see the \r on input by setting the PERLIO environment variable to :raw.
      set PERLIO=:raw perl -ne "print if /\r/" file.txt [displays lines]
      This also allows you to write to text files without getting \r inserted. But it still doesn't solve the -i problem. Which kinda looks like a bug.