Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to print "sample text\n" to a file and then printing another line and get two seperate lines. But the \n's are just turning out to be blocks, how can I solve this?

Replies are listed 'Best First'.
Re: printing \n to a file
by chromatic (Archbishop) on Oct 16, 2000 at 06:23 UTC
    It sounds like you may be on Windows, opening the file in Notepad.

    Change the line ending to "\r\n", and see if that helps.

    Otherwise, tell us which OS you're using and how you're viewing these files, and we may be more helpful.

      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?

      Sound to me like you're writing the file on a Unix file system and then transfering it to Windows using a binary file transfer mechanism. This article may shed a little more light on the problem.

      Otherwise, as chromatic says, give us more details where and how you're reading and writng these files and we can probably be more help.

      --
      <http://www.dave.org.uk>

      "Perl make the fun jobs fun
      and the boring jobs bearable" - me

        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.

RE: printing \n to a file
by Albannach (Monsignor) on Oct 16, 2000 at 17:48 UTC
    Just a data point here: Under WinNT 4.0 with Perl 5.6.0 (ActiveState's build 618), "\n" produces two bytes (hex 0d 0a) in an output text file. Every DOS or Win32 build of Perl I've used has had this behaviour as I recall.