in reply to Re: Is there a way to open a memory file with binmode :raw?
in thread Is there a way to open a memory file with binmode :raw?

This is very informative kcott, thanks :)

However, it doesn't help me understand why on Windows, when printing to a file-based file handle, an \n is printed by default as \r\n into the file (without any binmode or IO trickery, it just does so naturally.

However, the default record separator \r\n is not printed to a memory file based handle, it is printed only as \n. I would expect that regardless of type of handle, the default OS record separator would be used. I can't find anywhere that states this discrepancy between a real file and printing the exact same thing to a scalar reference acting as a file handle.

That, or I'm missing something very basic.

Replies are listed 'Best First'.
Re^3: Is there a way to open a memory file with binmode :raw? (\r, consistency)
by tye (Sage) on Oct 10, 2015 at 16:28 UTC

    The problem is only with your expectations. Did you know that, in Unix, writing "\n" also becomes "\r\n", by default, just not in ordinary files. For example, it does that when writing to a TTY (having the default configuration).

    Writing to a Perl scalar is not handled by the Windows clib, obviously. So there is no requirement that such writes emulate the default behavior of Windows' clib.

    "\r\n" is the default text record separator for Windows text files.

    - tye        

Re^3: Is there a way to open a memory file with binmode :raw?
by BrowserUk (Patriarch) on Oct 10, 2015 at 18:05 UTC

    CRLF translation is feature of the Windows file systems; not the Perl language. The PerlIO layers emulate it when writing to the Windows file system.

    One fairly typical usage of memory files is to reduce IO overheads by accumulating lines together into a single scalar and then write the entire file in one go.

    If Perl applied the CRLF translation when writing to the memory file; then when the scalar is written to the file, the file system (or file system emulation) would apply the CRLF translation a second time and you would end up with \r\r\n.

    Of course that could be avoided by applying the non default :raw layer to the actual output file; or by applying binmode; but that means extra steps are required.

    Better to only apply CRLF translations when actually writing to actual file system files and then the default behaviours work together to produce the desired result.


    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Is there a way to open a memory file with binmode :raw?
by kcott (Archbishop) on Oct 11, 2015 at 02:21 UTC

    Update: The accuracy of the information I linked to (perlport: Newlines) is in question. See tye's response to this node.

    In Perl, \n is a logical newline. It does not necessarily represent the single ASCII character whose decimal value is 10.

    Perhaps a read of "perlport: Newlines" will help clarify the situation for you.

    — Ken

      Yeah, perlport has caused more wrong conclusions than enlightenment on newlines in my experience. For example:

      In Perl, \n is a logical newline. It does not necessarily represent the single ASCII character

      In Perl, "\n" is actually always exactly one character. On an ASCII system, it is also always ASCII linefeed... except for the single case of old Macs, which took the unprecedented route of being "almost ASCII".

      "\n" is not much more a "logical" newline than "a" is a logical letter A. "a" is also always exactly one character and is also not always the ASCII lower-case letter A.

      - tye        

        Thanks for the feedback. I've updated my node.

        — Ken