Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

CRLF question

by Sary (Novice)
on Mar 08, 2011 at 08:34 UTC ( [id://891972]=perlquestion: print w/replies, xml ) Need Help??

Sary has asked for the wisdom of the Perl Monks concerning the following question:

So, been curious about socket programming and file handling. Than those CRLF, LF and more pop in the code. Tried scouring the web for definitions but could not find anything satisfying.

Could anyone throw me a bone about what those are? I know it has to do something with encoding and maybe end of line in text files..

Also seen it been used as a Module:

use CRLF;

HELP!! Thanks guys, the replies and examples you provided were great! Got my answers in some 20 minutes^^. awesome!

Replies are listed 'Best First'.
Re: CRLF question
by ELISHEVA (Prior) on Mar 08, 2011 at 09:09 UTC

    On a sheet of paper we're accustom to dividing the page up into lines. Data is just a run of bytes, but we still like the notion of "lines", so during the history of computing there have been a number of different characters and character strings that have been "standard" for marking the end of a logical line of input.

    On some systems, including systems that inherit from DOS, the end of line marker is two characters: a carriage return character followed by a line feed character. In your code this would be "\r\n". \r is the carriage return character and \n is the new line character. In technical documents this sequence is often referred to as CRLF. CR is the carriage return character. LF is the new line character.

    This two character sequence is duplication of the mechanical steps needed to create a new line on a typewriter or other printing device. If you have ever used an old manual typewriter you may remember that two stroke action: push the roller to the right (carriage return) and advance a line (a lever on the left).

    Of course, since this end of line is really just a logical end of line, we don't really need those two mechanical control characters. Other operating systems developed the convention that a new-line character was enough to represent the logical end of line. Computers working with data in memory don't need to push a roller back to the beginning of the line so CR is redundant. Consequently, unix and linux files use only a "\n"

    There are also other end-of-line conventions, for example, Mac Classic and IBM mainframes used 0x15 (CR). For the gory details see Newline.

    I know it has to do something with encoding ...Also seen it been used as a Module

    In Perl, CRLF shows up as part of IO. If you open a file handle in text mode, Perl automatically converts CRLF in the input stream to LF ("\n") when it reads in input. When it prints to a text mode file handle it does the reverse, converting LF to CRLF.

    You can find out whether your file handle is doing automatic conversion by calling PerlIO::get_layers. If one of the array elements is ":crlf", then your file handle is doing conversion for you.

    You can also gain explicit control over whether or not a file handle does the CRLF to LF conversions, by playing around with the second parameter of open or by using the binmode command.

    I'm not sure where you have seen it used as a module, i.e. use crlf. Perhaps another monk can inform us. A search on both http://perldoc.perl.org and CPAN came up dry for me.

Re: CRLF question
by ikegami (Patriarch) on Mar 08, 2011 at 08:47 UTC

    "CR" means "Carriage Return", character 13 (0x0D, 015) in ASCII-based encodings.

    "LF" means "Line Feed", character 10 (0x0A, 012) in ASCII-based encodings.

    "CRLF" refers to a CR followed by a LF.

    I don't know of this CRLF module. It's not on CPAN.

Re: CRLF question
by johngg (Canon) on Mar 08, 2011 at 10:57 UTC

    This node gives a couple of examples of dealing with CR & LF in the context of chomping to remove line terminators. The second example, opening a file in ":crlf" mode, could be what you need.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://891972]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-25 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found