http://www.perlmonks.org?node_id=971863


in reply to Native newline encoding

Besides using a table to infer it from $^O, is there any other way to detect it?

It's not neat, but you can actually write a newline to a file and then read it in binmode:

use warnings; use strict; open FH, '>', 'out.txt' or die $!; print FH "\n"; close FH; open FH, '<', 'out.txt' or die $!; binmode(FH); my $stuff = do { local $/; <FH> }; print unpack('H*', $stuff), "\n";

Tested on Windows, where it prints 0d0a and on Linux where it prints 0a.

You could clean it up, use File::Temp... whatever. But, unfortunately, you can't get by with an in memory file or IO::Scalar.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: Native newline encoding
by salva (Canon) on May 23, 2012 at 08:33 UTC
    Unfortunately my module may run in environments where creating files and writing to them may be forbidden. Using an in-memory file doesn't seem to work.
Re^2: Native newline encoding
by BrowserUk (Patriarch) on May 22, 2012 at 23:46 UTC

    And what happens when the OP tries to use that information to process the line endings in this text file?

    C:\test>od -t x1 nonsense.txt 0000000 ff fe 55 00 6e 00 20 00 62 00 65 00 61 00 75 00 0000020 20 00 6a 00 6f 00 75 00 72 00 20 00 61 00 75 00 0000040 20 00 6d 00 69 00 6c 00 69 00 65 00 75 00 20 00 0000060 64 00 65 00 20 00 6c 00 61 00 20 00 6e 00 75 00 0000100 69 00 74 00 2c 00 20 00 0d 00 0a 00 64 00 65 00 0000120 75 00 78 00 20 00 6d 00 6f 00 72 00 74 00 73 00 0000140 20 00 6c 00 65 00 73 00 20 00 67 00 61 00 72 00 0000160 e7 00 6f 00 6e 00 73 00 20 00 73 00 65 00 20 00 0000200 6c 00 65 00 76 00 61 00 20 00 70 00 6f 00 75 00 0000220 72 00 20 00 6c 00 75 00 74 00 74 00 65 00 72 00 0000240 20 00 63 00 6f 00 6e 00 74 00 72 00 65 00 2c 00 0000260 20 00 0d 00 0a 00 64 00 6f 00 73 00 20 00 e0 00 0000300 20 00 64 00 6f 00 73 00 20 00 69 00 6c 00 73 00 0000320 20 00 73 00 65 00 20 00 73 00 6f 00 6e 00 74 00 0000340 20 00 61 00 66 00 66 00 72 00 6f 00 6e 00 74 00 0000360 e9 00 73 00 2c 00 20 00 0d 00 0a 00 61 00 20 00 0000400 61 00 70 00 70 00 65 00 6c 00 e9 00 20 00 6c 00 0000420 65 00 75 00 72 00 20 00 e9 00 70 00 e9 00 65 00 0000440 73 00 20 00 65 00 74 00 20 00 61 00 62 00 61 00 0000460 74 00 74 00 75 00 20 00 75 00 6e 00 20 00 64 00 0000500 65 00 20 00 6c 00 27 00 61 00 75 00 74 00 72 00 0000520 65 00 2c 00 20 00 0d 00 0a 00 6c 00 27 00 75 00 0000540 6e 00 20 00 e9 00 74 00 61 00 69 00 74 00 20 00 0000560 61 00 76 00 65 00 75 00 67 00 6c 00 65 00 20 00 0000600 65 00 74 00 20 00 6c 00 27 00 61 00 75 00 74 00 0000620 72 00 65 00 20 00 70 00 61 00 73 00 2c 00 20 00 0000640 0d 00 0a 00 76 00 6f 00 69 00 72 00 20 00 73 00 0000660 69 00 20 00 69 00 6c 00 73 00 20 00 6f 00 6e 00 0000700 74 00 20 00 63 00 68 00 6f 00 69 00 73 00 69 00 0000720 20 00 75 00 6e 00 20 00 6d 00 61 00 6e 00 6e 00 0000740 65 00 71 00 75 00 69 00 6e 00 20 00 70 00 6f 00 0000760 75 00 72 00 20 00 75 00 6e 00 20 00 61 00 72 00 0001000 62 00 69 00 74 00 72 00 65 00 2e 00 0001014

    Oh dear!


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      And what happens when the OP tries to use that information to process the line endings in this text file?

      Why are you assuming he would? Or even that he would use it to process any line endings? He didn't say that's what his intention was. He just asked about how to find that information. Microsoft and you might want to try to hide that info but I don't see the point. And I do see reasons why one might want to get it.

      -sauoq
      "My two cents aren't worth a dime.";
        Why are you assuming he would? Or even that he would use it to process any line endings? He didn't say that's what his intention was.

        So, your assumption is that he's not going to use the information; just discover it and then what? Impress his users with it.

        Microsoft and you might want to try to hide that info

        Oh dear. Hide information that is common knowledge; and has been common knowledge for what? 30 years or more. Man, I thought you were better than this, but you've really lost the plot.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?