Trouble with printing newlines to a file

Discussion in 'Perl Misc' started by shaneal, May 12, 2007.

  1. shaneal

    shaneal Guest

    I want to be able to print just an "LF" to a file.

    First, to clear up any ambiguity, I'm going to call the ASCII
    character with a hex code of 0A and a decimal code of 10 an "LF". I'm
    also going to call the ASCII character with a hex code of 0D and a
    decimal code of 15 a "CR".

    As a simple test case:

    open TEST, ">", "test.data";
    select TEST;
    print "A" . chr(hex("0A")) . "B" . "\n" . "C" . "\r" . "D";
    close TEST;

    Logically, when I look at the test.data file with a hex editor I'd
    expect just a "LF" between the A and B, but instead there are is a
    "CR" followed by an "LF."

    Here is what a hex editor shows is in "test.data" (first line is hex,
    second line are chars):

    41 0D 0A 42 0D 0A 43 0D 44
    A CR LF B CR LF C CR D

    I was expecting only a "LF" (i.e., 0A) between the "A" (41) and
    "B" (42) ...

    If it helps this is Perl 5.8.8 (ActiveState Perl Build 820) on Windows
    XP.

    Thanks for any advice you may be able to give me.
     
    shaneal, May 12, 2007
    #1
    1. Advertisements

  2. shaneal

    Dr.Ruud Guest

    shaneal schreef:
    perldoc -f binmode

    (search on 'Windows' in there)
     
    Dr.Ruud, May 12, 2007
    #2
    1. Advertisements

  3. shaneal

    Peter Wyzl Guest

    Given that it's Windows, you may need to binmode the file so the OS doesn't
    make wrong assumptions about your file.

    open TEST, ">", "test.data";
    select TEST;
    binmode TEST;

    perldoc binmode

    P
    Of course I could be on the wrong track entirely....
     
    Peter Wyzl, May 12, 2007
    #3
  4. shaneal

    shaneal Guest

    thanks you both very much.

    i wasn't familiar with binmode. After reading some docs, it worked
    flawlessly
     
    shaneal, May 12, 2007
    #4

  5. binmode is not a eunich's term, it is a Perl term.

    It is the same term when used on other "operating systems"
    too (such as the OP's).
     
    Tad McClellan, May 12, 2007
    #5
  6. shaneal

    Dr.Ruud Guest

    A. Sinan Unur schreef:

    Using either "\n" of chr(10) or "\x0A" or "\x{0A}" or "\012" or "\cJ"
    (etc.) seems to make no difference (on Windows, have not tested OS-X).

    So just don't forget to call binmode, if the platform's line ending is
    not what you want:


    C:\>perl -we "print chr(10)" > test.out
    C:\>debug test.out
    -d
    14C6:0100 0D 0A 00 00 00 00 00 00-[...]
    -r
    AX=0000 BX=0000 CX=0002 DX=0000[...]



    C:\>perl -we "binmode STDOUT; print chr(10)" > test.out
    C:\>debug test.out
    -d
    14C6:0100 0A 0A 00 00 00 00 00 00-[...]
    -r
    AX=0000 BX=0000 CX=0001 DX=0000[...]


    (the value in CX is the size of the file)
     
    Dr.Ruud, May 13, 2007
    #6
  7. Yes, "\n" is the same as "\x0A" on Windows. I think it's the same on
    MacOS X, but it was "\x0D" on MacOS <= 9.

    I don't know what EBCDIC-based versions of perl use: NL ("\x15") is
    probably more natural than LF ("\x25") there.

    hp
     
    Peter J. Holzer, May 13, 2007
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.