http://qs321.pair.com?node_id=1161409


in reply to Re^3: Regular expressions across multiple lines
in thread Regular expressions across multiple lines

This looks mainly right, but with some quibbles.

1) Correct, the standard text line endings are:
Unix: <LF> - Line Feed
Windows: <CR><LF> - Carriage Return, Line Feed
Network Socket: <CR><LF> - Carriage Return, Line Feed
Old Mac: <CR> - Carriage Return.

That a standard network socket (of course even on Unix) uses "Windows" line endings may be news to some.

2) The chomp description is not 100% clear. When reading in text mode and with the default input record separator of "\n", chomp() will remove any of the line endings that "\n" could mean on any of these 3 platforms between Unix and Windows. The C function getline() will work similarly. Reading a Windows file on Unix will work fine with this text oriented C read function.

3) Some ancient Unix functions like lp (line print) will not work with Windows line endings. Perl is fine, but lp not. In that case: while(<>){chomp;print;} will set things right. I have used this many times on Unix to convert a mixed file to <LF> endings and vice versa on Windows to convert to <CR><LF>. Although my Windows programs just don't seem to care.

4) I don't know how these test cases were generated. There is no way to do that without being in Perl bin mode or writing a C program.

Update: well it appears that Perl doesn't like old Mac endings on my Windows XP machine. This does work with the <LF> ending. So something like "works between Unix and Windows" may be closer to the truth (dual platform) rather than "multi-platform". On Unix, Perl has to be able to read from both hard disk files and network sockets which have different line endings.

#!/usr/bin/perl use warnings; use strict; open OUT, '>', "unixending.txt" or die "$!"; binmode OUT; print OUT pack "C8", 0x41,0x42,0x43,0x0A,0x44,0x45,0x46,0x0D; close OUT; open IN, '<', "unixending.txt" or die "$!"; while (<IN>) { chomp; print "\"$_\"\n"; } __END__ "ABC" ## fine for Unix <LF> 0x0A "DEF ## didn't work for old MAC <CR> 0x0D "