Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Carrige Return and Line Feed in Perl.

by Eliya (Vicar)
on Mar 01, 2011 at 17:10 UTC ( #890789=note: print w/replies, xml ) Need Help??

in reply to Carrige Return and Line Feed in Perl.

2.Is CR the newline character for windows? and LF the newline character for linux?
3.Is CR == "\r\n" and LF == "\n" in perl?

\r = CR = carriage return = ASCII code 13 (decimal), 015 (octal), 0d (hex)
\n = LF = line feed = ASCII code 10 (decimal), 012 (octal), 0a (hex)

On Windows, the combination of those two control characters, i.e. \r\n, is used to indicate a newline, while on Linux/Unix, a single \n is used as newline.

(To simplify things, this ignores old-style Mac semantics — see the already mentioned link for details.)

4.What about Control-M, that appears at the end of windows generated files before dos2unixing them, is that character just "\r"?

See Caret Notation.  More specifically, Control-M, or ^M, is the same as \r, because \r is ASCII code 13, and M is the 13th character in the alphabet.

6.I assumed that the "\n" is an LF ascii map is "\r" a CR ascii map?

Not sure what you mean by that.

7.When I used to get some information via an ssh server hosted on a appliance, a lot of the lines returned were terminated by "\r\n", what character is that?

Generally, \r\n is a Windows newline (see above), but it's also used in some network protocols to indicate newline (aiming to be portable).

8.Is there a difference between a 'line terminator' in the terminal and in a file?


That said, as long as you do not operate across platforms, you usually need not worry about line terminator differences, because the PerlIO layer ":crlf" automatically converts newlines to and from Perl's internally used \n when reading and writing from/to file handles — in case a conversion is required, such as on Windows.  I.e. (on Windows, by default), \r\n is translated to \n on input, and on output, \n is translated to \r\n.  But if you need to, you can change that behavior with binmode or open.  For example, to transparently read/write Windows files on Unix, you can push the :crlf layer on the respective file handle's layer stack.

Replies are listed 'Best First'.
Re^2: Carrige Return and Line Feed in Perl.
by dannyd (Sexton) on Mar 01, 2011 at 20:57 UTC

    All questions answered in 3 posts ^^ wow..didn't expect that!

    I guess a lot of them were repetitions, but to my mind before the posts they were its still a wow...

    Thanks toolic, moritz and Eliya for all the information, some of this stuff has been plaguing me for a while now.

    Note to self : Start chomping away, and search perldocs first before posting :)

      Also a bit of history: Wiki CRLF history. This is extra info - some folks find it interesting.

      Many moons ago I worked with these ASR33 teletypes. In order to see one now, you'd have to go to a museum or watch an old black and white film. These things used paper tape which had lubricating oil embedded in the tape. On the keyboard, on the right, there were 3 keys arranged together: Carriage return (CR), Line Feed (LF) and rubout (NULL) (not just a single "return" or "enter" button). The paper tape was 8 bits wide, a NULL meant to punch holes in all 8 positions. The normal sequence to end a line was: CR, LF, Rubout.

      I'm not sure that I buy all of the explanation in the wiki article. The ASR33 was a very stupid thing and I think part of the reason was to just have a separate key for each mechanical action (eg separate CR from LF). This also allowed low resolution pictures of a sort to be printed by repeatedly printing over the same line (no LF). I saw many pictures printed like this so, the Wiki theory that LF was needed as time delay for the CR mechanics is doubtful - there was hardware flow control. The lubrication for the mechanical fingers came from the tape itself, so the periodic Rubout after every line served to keep all of the fingers well lubricated. A series of rubouts served to indicate the preamble and postamble to a tape. Nasty stuff as all the folks dealing with these oil soaked tapes wound up with grease stains in their shirt pockets! We viewed optical tape readers as a major technological advance! No more oil in the tape and therefore no more grease stains in the shirt pockets!

      On a more practical side of things, I exchange programs from my Windows box to Unix and vice-versa. Last month I wrote 2 Perl files on Unix with vi and one Perl file on Windows. Perl on either machine didn't care and the program ran on both machines (Perl doesn't care for its own program). I haven't had trouble with exchanging data files when Perl is doing the reading.

      I use TextPad (a shareware program) for my Windows program editor and it doesn't care either. The trouble happens when you are exchanging data with something like Windows NotePad which does care (it cannot deal with just LF as a "newline" character). However a simple Perl program that chomps and then re-writes the line with a "\n" seems to set things right (results in \r\n when run on Windows and just \n when run on Unix).

      So by and large, I figure that Perl did about as well as can be done with this mess.

      If you are say hashing to disk with a fixed record size in bytes, this is certainly a consideration, but I've written multi-platform programs that work fine. This is a detail that matters, but there are solutions. But for the most part, this is not important at all, except when the output file is going to be used by a non-Perl application that can't deal with different line endings.


        FYI: I can corroborate the Wikipedia story that if you printed a character immediately after the CR that it appears as a smudge during the carriage return. It's a remarkably clever, but dumb device. I can still imagine the sound of our school's teletype.

        Boring story: When I was in High School, my friend Tim came up to me, and says "Hey, Marco! We have a computer at our school!" Needless to say, this was when having a computer at school would be a remarkable thing. Anyway, he leads me to Mr. Lamkin's math class, and shows me the teletype. It turns out that we didn't have a computer at school, but all the schools in the district were issued a teletype, acoustic MODEM and accounts on the timeshare (an HP-2000, if I recall correctly).

        There were no computer classes at our school, so when Mr. Lamkin saw that we were interested in it, he gave us accounts on the computer and said we could use it as much as we liked.

        We liked quite a lot! So we would often cut other classes to work on the computer in his math class, while he was teaching Trigonometry and Pre-calculus. As the teletype was a noisy beast, he got tired of hearing it during class. But rather than revoking our computer privileges, he got the school to move the teletype to an unused storage room, and gave us access to it there.

        A few other guys were interested, and soon afterwards, we had a gang of us teaching ourselves computer programming.

        One other fun fact: One of the guys in the group was my boss up until a few months ago.

        Ah, well, end of old-man reminiscences...


        When your only tool is a hammer, all problems look like your thumb.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://890789]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2020-06-07 06:09 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (42 votes). Check out past polls.