in reply to Can't get rid of \r

My limited understand is this: When you're on Windows, \n actually means CRLF.

So the safest approach is to explicitly split on CRLF aka \x0D\x0A (or set $/ to that) and to explicitly join on LF aka \x0A.

I'd do something like this:

open my $in, '<:raw', $in_filename or die ...; open my $out, '>:raw', $out_filename or die ...; while (<$in>){ s/\x0D\x0A\z/\x0A/; print $out $_; } close $in or die ..; close $out or die ... ;

Replies are listed 'Best First'.
Re^2: Can't get rid of \r
by rovf (Priest) on Sep 11, 2008 at 15:59 UTC
    When you're on Windows, \n actually means CRLF. So the safest approach is to explicitly split on CRLF
    Not exactly. The translation happens during reading/writing. Once you have read your file into memory, \n is \x0A on every platform (otherwise, length("\n") would be 2 on Windoze. So my intention was to suppress the translation on writing, by setting the file handle to binmode.
    open my $out, '>:raw', $out_filename

    I think I will try this; after all, this is a good occasion to get familiar with IO layers in Perl. If it suppresses \n conversion, the s/// won't be necessary. I'll post my findings.

    Still, it would be interesting to know why my binmode() did not worked. Is binmode not supposed to be used in that way, or is there a bug in the IO::File::binmode implementation?

    -- 
    Ronald Fischer <ynnor@mm.st>