Newlines on Windows

Discussion in 'Perl Misc' started by void.no.spam.com@gmail.com, Sep 27, 2008.

  1. Guest

    I thought that on Windows, if you want to output a carriage return,
    you have to use \r\n instead of just \n. So in my perl script that
    generates a file, I put a \r\n after every line, but when I open the
    file in an editor, I see a ^M after each line. This is with
    ActivePerl 5.8.8.
     
    , Sep 27, 2008
    #1
    1. Advertising

  2. schrieb:
    > I thought that on Windows, if you want to output a carriage return,
    > you have to use \r\n instead of just \n.


    So it's like in PHP. In Perl you haven't to do so. Just use \n and Perl
    takes care for the rest, on Windows, Linux, Macintosh and all the rest.
     
    Chris Haffenstedt, Sep 27, 2008
    #2
    1. Advertising

  3. "" <> wrote:
    >I thought that on Windows, if you want to output a carriage return,
    >you have to use \r\n instead of just \n.


    Neither nor. You are confusing two different layers.

    To output a carriage return you have to print the ASCII controll
    character 13 (DEZ) or 0x0D (HEX) or short "\r". That is by definition
    the CR character for ASCII which in turn is a subset of Windows-1252,
    ISO-Latin-1, and many other encodings.

    "\n" on the other hand in Perl is a logical line end, which will be
    translated automatically into the correct line end sequence for the
    current OS, be it 0x0A for unixoide OSes, 0x0A 0x0D for Windows, or 0x0D
    for Mac.

    So your combination \r\n on Windows will result in 0x0D 0X0A 0x0D.
    Probably not what you want.

    > So in my perl script that
    >generates a file, I put a \r\n after every line, but when I open the
    >file in an editor, I see a ^M after each line. This is with


    Well, you got what you added.

    jue
     
    Jürgen Exner, Sep 27, 2008
    #3
  4. Guest

    On Sep 27, 3:34 pm, Jürgen Exner <> wrote:
    > "" <> wrote:
    > >I thought that on Windows, if you want to output a carriage return,
    > >you have to use \r\n instead of just \n.

    >
    > Neither nor. You are confusing two different layers.
    >
    > To output a carriage return you have to print the ASCII controll
    > character 13 (DEZ) or 0x0D (HEX) or short "\r". That is by definition
    > the CR character for ASCII which in turn is a subset of Windows-1252,
    > ISO-Latin-1, and many other encodings.
    >
    > "\n" on the other hand in Perl is a logical line end, which will be
    > translated automatically into the correct line end sequence for the
    > current OS, be it 0x0A for unixoide OSes, 0x0A 0x0D for Windows, or 0x0D
    > for Mac.
    >
    > So your combination \r\n on Windows will result in 0x0D 0X0A 0x0D.
    > Probably not what you want.
    >
    > > So in my perl script that
    > >generates a file, I put a \r\n after every line, but when I open the
    > >file in an editor, I see a ^M after each line.  This is with

    >
    > Well, you got what you added.
    >
    > jue


    Thanks for the explanation.
     
    , Sep 27, 2008
    #4
  5. Tim Greer Guest

    wrote:

    > I thought that on Windows, if you want to output a carriage return,
    > you have to use \r\n instead of just \n. So in my perl script that
    > generates a file, I put a \r\n after every line, but when I open the
    > file in an editor, I see a ^M after each line. This is with
    > ActivePerl 5.8.8.


    This is an issue with what you use to edit the file. It's inserting
    hidden line feed characters (junk) in the script.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Sep 27, 2008
    #5
  6. * wrote in comp.lang.perl.misc:
    >I thought that on Windows, if you want to output a carriage return,
    >you have to use \r\n instead of just \n. So in my perl script that
    >generates a file, I put a \r\n after every line, but when I open the
    >file in an editor, I see a ^M after each line. This is with
    >ActivePerl 5.8.8.


    See `perldoc perlport` for an in-depth discussion of line endings.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
     
    Bjoern Hoehrmann, Sep 27, 2008
    #6
  7. On 2008-09-27 19:47, <> wrote:
    > On Sep 27, 3:34 pm, Jürgen Exner <> wrote:
    >> "" <> wrote:
    >> >I thought that on Windows, if you want to output a carriage return,
    >> >you have to use \r\n instead of just \n.

    >>
    >> Neither nor. You are confusing two different layers.
    >>
    >> To output a carriage return you have to print the ASCII controll
    >> character 13 (DEZ) or 0x0D (HEX) or short "\r". That is by definition
    >> the CR character for ASCII which in turn is a subset of Windows-1252,
    >> ISO-Latin-1, and many other encodings.
    >>
    >> "\n" on the other hand in Perl is a logical line end, which will be
    >> translated automatically into the correct line end sequence for the
    >> current OS, be it 0x0A for unixoide OSes, 0x0A 0x0D for Windows, or 0x0D
    >> for Mac.
    >>
    >> So your combination \r\n on Windows will result in 0x0D 0X0A 0x0D.
    >> Probably not what you want.

    [...]
    >
    > Thanks for the explanation.


    It is worth noting that the translation Jürgen talks about happens only
    during file I/O. Inside of a perl script, "\n" is always a single
    character ("\015" on old MacOS, "\012" on Unix, Windows, and (I think)
    also on MacOS X). When writing to a file with the :crlf I/O layer enabled
    (which is the default on Windows, but you can turn it off with binmode
    or an argument to open), the single "\n" is converted to "\015\012". The
    reverse conversion is done when reading a file with the :crlf layer.

    (Perl can do other conversions on I/O, too, e.g., character set
    conversions, compression, etc.)

    hp
     
    Peter J. Holzer, Sep 28, 2008
    #7
  8. "Peter J. Holzer" <> wrote:
    >It is worth noting that the translation Jürgen talks about happens only
    >during file I/O. Inside of a perl script, "\n" is always a single
    >character ("\015" on old MacOS, "\012" on Unix, Windows, and (I think)
    >also on MacOS X). When writing to a file with the :crlf I/O layer enabled
    >(which is the default on Windows, but you can turn it off with binmode
    >or an argument to open), the single "\n" is converted to "\015\012". The
    >reverse conversion is done when reading a file with the :crlf layer.


    To be crystal clear on that: did you mean "writing to a file" or
    "writing to a file handle"?
    Like in writing to STDOUT, but having the shell redirect into a file?

    jue
     
    Jürgen Exner, Sep 28, 2008
    #8
  9. Dr.Ruud Guest

    Jürgen Exner schreef:

    > your combination \r\n on Windows will result in 0x0D 0X0A 0x0D.


    On Windows, \n is CR-LF, not LF-CR.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 28, 2008
    #9
  10. On 2008-09-28 09:38, Jürgen Exner <> wrote:
    > "Peter J. Holzer" <> wrote:
    >>It is worth noting that the translation Jürgen talks about happens only
    >>during file I/O. Inside of a perl script, "\n" is always a single
    >>character ("\015" on old MacOS, "\012" on Unix, Windows, and (I think)
    >>also on MacOS X). When writing to a file with the :crlf I/O layer enabled
    >>(which is the default on Windows, but you can turn it off with binmode
    >>or an argument to open), the single "\n" is converted to "\015\012". The
    >>reverse conversion is done when reading a file with the :crlf layer.

    >
    > To be crystal clear on that: did you mean "writing to a file" or
    > "writing to a file handle"?


    File handle.

    hp
     
    Peter J. Holzer, Sep 28, 2008
    #10
  11. David Combs Guest

    In article <>,
    Peter J. Holzer <> wrote:
    >On 2008-09-28 09:38, Jürgen Exner <> wrote:
    >> "Peter J. Holzer" <> wrote:
    >>>It is worth noting that the translation Jürgen talks about happens only
    >>>during file I/O. Inside of a perl script, "\n" is always a single
    >>>character ("\015" on old MacOS, "\012" on Unix, Windows, and (I think)
    >>>also on MacOS X). When writing to a file with the :crlf I/O layer enabled
    >>>(which is the default on Windows, but you can turn it off with binmode
    >>>or an argument to open), the single "\n" is converted to "\015\012". The
    >>>reverse conversion is done when reading a file with the :crlf layer.

    >>
    >> To be crystal clear on that: did you mean "writing to a file" or
    >> "writing to a file handle"?

    >
    >File handle.
    >
    > hp


    I'm a bit puzzled by your question, file or file-handle.

    Suppose he'd answered "writing to a file".

    How would your answer be different?

    Thanks,

    David
     
    David Combs, Oct 14, 2008
    #11
  12. On 2008-10-14 03:25, David Combs <> wrote:
    > In article <>,
    > Peter J. Holzer <> wrote:
    >>On 2008-09-28 09:38, Jürgen Exner <> wrote:
    >>> "Peter J. Holzer" <> wrote:
    >>>>It is worth noting that the translation Jürgen talks about happens
    >>>>only during file I/O. Inside of a perl script, "\n" is always a
    >>>>single character ("\015" on old MacOS, "\012" on Unix, Windows, and
    >>>>(I think) also on MacOS X). When writing to a file with the :crlf
    >>>>I/O layer enabled (which is the default on Windows, but you can turn
    >>>>it off with binmode or an argument to open), the single "\n" is
    >>>>converted to "\015\012". The reverse conversion is done when reading
    >>>>a file with the :crlf layer.
    >>>
    >>> To be crystal clear on that: did you mean "writing to a file" or
    >>> "writing to a file handle"?

    >>
    >>File handle.

    >
    > I'm a bit puzzled by your question, file or file-handle.
    >
    > Suppose he'd answered "writing to a file".
    >
    > How would your answer be different?


    I'm not quite sure who you mean by "you" and "he". But anyway:

    It is conceivable that perl behaves differently for file handles which
    refer to files (i.e., byte streams stored on a random access medium such
    as a disk) and file handles which refer to other objects (pipes,
    sockets, terminals, ...). In fact, perl does make a similar distinction
    for buffering (file handles referring to terminals are by default
    line-buffered, file handles referring to sockets are unbuffered, and
    everything else is block-buffered). However, for I/O layers there is no
    such distinction - what I wrote is true for all kinds of file handles,
    not just for file handles referring to files.

    hp
     
    Peter J. Holzer, Oct 17, 2008
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. headware

    newlines in textboxes

    headware, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    358
    Girish Bharadwaj
    Sep 22, 2004
  2. Newlines in Datagrid

    , Sep 6, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,066
    RenyMatt
    May 4, 2009
  3. Tom
    Replies:
    5
    Views:
    317
  4. Jamis Buck

    YAML and Windows newlines

    Jamis Buck, Jun 17, 2004, in forum: Ruby
    Replies:
    2
    Views:
    228
    Jamis Buck
    Jun 18, 2004
  5. Wes Gamble
    Replies:
    7
    Views:
    108
    Wes Gamble
    Dec 14, 2006
Loading...

Share This Page