0
open OUT, ">output.txt";
print OUT "Hello\nWorld";

When I run the above perl code in a Unix system and then transfer output.txt to Windows and open it in Notepad it shows as:

HelloWorld

What do I need to do to get the newlines displaying properly in Windows?

CJ7
  • 22,579
  • 65
  • 193
  • 321

4 Answers4

3

Text file line endings are platform-specific. If you're creating a file intended for the Windows platform then you should use

open OUT, '>:crlf', 'output.txt' or die $!;

Then you can just

print OUT "Hello\nworld!\n";

as normal

The :crlf PerlIO layer is the default for Perl executables built for Windows, so you don't need to add it to code that will create files for its intended platform. For portable software you can check the host system by examining the built-in variable $^O

Borodin
  • 126,100
  • 9
  • 70
  • 144
1

Windows uses carriage-return + linefeed:

print OUT "Hello\r\nWorld";
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
kizeloo
  • 183
  • 8
  • Will the `\r` have any effect in Unix? – CJ7 Feb 08 '16 at 05:23
  • @CJ7 Yes, it will. What exactly are you trying to do? – ThisSuitIsBlackNot Feb 08 '16 at 05:54
  • @ThisSuitIsBlackNot When I ran the above code in Unix it had the same result as doing `print OUT "Hello\nWorld";` – CJ7 Feb 08 '16 at 22:22
  • @CJ7 It's not the same. For example, `echo -e '#!/bin/sh\r\necho foo' > foo && chmod 755 foo && ./foo` will give you `/bin/sh^M: bad interpreter: No such file or directory` (same if you write the file with perl). Now do `perl -pe's/$/bar/' foo`, adding "bar" to the end of each line of the same file; when printed, the first line is `barbin/sh`, not `#!/bin/shbar` as you might expect. – ThisSuitIsBlackNot Feb 08 '16 at 22:51
0

I wrote the File::Edit::Portable module that eliminates these problems. Although you can use it to write (along with many other things), you only need the read() functionality in this case.

Install the module, and at the top of your script, add:

use File::Edit::Portable;

When opening/reading the file, you can just do:

my $rw = File::Edit::Portable->new;

my $fh = $rw->read('file.txt');

No matter what the line endings are or what platform you're on, it does all of the cross-platform work in the background so you don't have to. That way, you can open the file on any system, regardless of what line endings you've decided to use, and it just works.

stevieb
  • 9,065
  • 3
  • 26
  • 36
  • Cool module, although it might help to explain that programs that are unaware of line ending issues will only work with one or the other; based on their comment on kizeloo's answer, it sounds like the OP expects to be able to write a file that will be interpreted the same by all programs on both *nix and doze. – ThisSuitIsBlackNot Feb 08 '16 at 15:54
  • While this is a useful module, it doesn't seem to be relevant to the OP's question as s/he is talking about *creating* a file for the first time on a *nix system intended to be viewed correctly on Windows using Notepad. – Sinan Ünür Feb 14 '16 at 19:20
0

Newline handling is editor specific (there are a number of answers that claim it is OS specific, but that is, in real life, not true in general). However, it is true that on DOSish systems, longstanding convention is to use CRLF to indicate EOL (see also Why is fwrite writing more than I tell it to?)

If you try to open this file in any other editor than Notepad, you will notice that the text is properly displayed on two lines, with an indicator in a status bar or some other place that the file is opened in Unix mode or LF mode.

Unless you intend your file solely for viewing with Notepad, you don't have anything to worry about. Every other tool on Windows will deal fine with it.

However, Notepad does expect a CRLF sequence to mark the end of each line. If you do want to cater for it, then you can just output "\r\n" as @kizeloo suggests. I do prefer to use output layers when they are necessary.

Note that if you try to view such a file using an editor that requires a single LF to signify EOL, you may see ^Ms or other characters denoting the CR.

Community
  • 1
  • 1
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339