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

Hi, I am wondering whether output generated by the following scripts are same on Linux and Windows machines.
use strict; use warnings; my $output = "output.txt"; die "can't open output file: $!" unless open FH, ">$output"; printf(FH "%s\n", "there won't be any junk characters in this version" +); close FH;
use strict; use warnings; my $output = "output2.txt"; die "can't open output file: $!" unless open FH, ">$output"; printf(FH "%s\r\n", "I wonder any junk charcter will be printed to the + file on windows"); close FH;
Can you please suggest which one is more portable?

Replies are listed 'Best First'.
Re: Difference between using \r\n and \n on Linux and Windows machines
by bart (Canon) on Jun 09, 2005 at 06:55 UTC
    Use the same version as on Linux, on Windows. The output layer for the file handle FH will, when in text mode (i.e. when binmode hasn't been used on it, like here) convert every "\n" character to the CR+LF sequence — or, like you typed, "\r\n", at least on Windows and Linux, as this notation isn't very portable.

    So, if you insert a "\r" character yourself, you will end up with two because perl inserts one itself next to the "\n". So don't do that.

    p.s. If you want to create a Unix text file on Windows, you can do

    binmode FH;
    before you print anything to it, and this conversion is prevented, you'll get an identical result on Windows as on Linux.