Newlines
On most operating systems, lines in files are terminated by one
or two characters that signal the end of the line. The characters vary
from system to system. Unix traditionally uses \012 (that is, the octal 12 character in
ASCII), one type of DOSish I/O uses \015\012, and the pre-Unix Macs used to use
\015. Perl uses \n to represent a “logical” newline,
regardless of platform. In DOSish Perls, \n usually means \012, but when accessing a file in “text
mode”, it is translated to (or from) \015\012, depending on whether you’re
reading or writing. Unix does the same thing on terminals in canonical
mode. \015\012 is commonly referred
to as CRLF.
Because DOS distinguishes between text files and binary files,
DOSish Perls have limitations when using seek and tell on a file in “text mode”. For best
results, only seek to locations
obtained from tell. If you use
Perl’s built-in binmode function on
the filehandle, however, you can usually seek and tell with impunity.
A common misconception in socket programming is that \n will be \012 everywhere. In many common Internet
protocols, \012 and \015 are specified, and the values of Perl’s
\n and \r are not reliable since they vary from
system to system:
print SOCKET "Hi there, client!\015\012"; # right print SOCKET "Hi there, client!\r\n"; # wrong
However, using \015\012 (or
\cM\cJ, or \x0D\x0A) can be tedious and unsightly, as
well as confusing to those maintaining the code. The Socket module supplies some Right Things for those who want ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access