in reply to RE: Re: printing \n to a file
in thread printing \n to a file

Surely Perl should take care of those translations for you. You don't have to change the line end characters in a script when you move it from Unix to Windows (or vice versa) do you?

Yes you do. (Note that perl itself understands unix line-end speak when reading in a script, but that's a different beast)

Perl isn't going to try and automatically translate, because it doesn't know WHY you are writing what you are writing. It's very possible that you DON'T want the \r automatically inserted/deleted (For example, this would break all the Dos<->Unix one liner converters.)

Update: As tye points out below, I missed the boat here.

Replies are listed 'Best First'.
(tye)Re: printing \n to a file
by tye (Sage) on Oct 16, 2000 at 18:27 UTC

    No, you don't. Unless you do binmode(FILEHANDLE), every "\n" you write to FILEHANDLE will be turned (by the C run-time library) into "\r\n" before it reaches the file. If you want to write a file that contains "\n" without the "\r", then have to use bindmode.

            - tye (but my friends call me "Tye")
      To avoid this confusion entirely, never use \n unless you are referring to the ASCII newline sequence, whatever that may be on your specific OS. If you truly want a binary ^M or ^J, consider using the \cM or \cJ codes.

        This makes sense to me as a self-documenting code practice (documenting what intent the programmer had when sending that newline to a file). However, I'd like to note that it won't fix the problem since, just like "\n" is silently turned into "\r\n" under Windows, "\cJ" will silently be turned into "\cM\cJ", since these are identical strings under Win32 (and most Perl platforms). In MacOS, "\n" is "\cM" and "\r" is "\cJ".

                - tye (but my friends call me "Tye")