Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl New Line separator issue

Tags:

newline

perl

I have a file that uses CR/LF to separate records, but individual records sometimes contain a LF.

while (<$in>)
{ 
    #extract record data
}

I am trying to read this code as above and this (as I would expect) splits the records that contain a LF only. I would however have expected that a reassigned $/ would resolve this issue but it does appear to cause the complete file to me read in one iteration.

$/ = "\r\n";
while (<$in>)
{ 
    #extract record data
}

Anyone here who can suggest a working solution?

I am using Activestate Perl on Windows.

like image 845
Hans Avatar asked Jul 09 '12 08:07

Hans


3 Answers

On windows, perl converts the incoming CRLF line endings to LF only, making a distinction between CRLF and LF impossible by reading in the data as text (perlport). Therefore, you have to read your data in binary mode using binmode on your file-handle:

binmode($in);

After that, you can set the input record separator to "\015\012" and read-in your records as usual:

$/ = "\015\012";
while (<$in>) {
    ...
}

greets, Matthias

PS: I have no chance to test that locally, at the moment, so I regret if it does not work.

like image 79
Matthias Avatar answered Oct 23 '22 01:10

Matthias


Try setting $/ to "\n". From Newlines in perlport:

Perl uses \n to represent the "logical" newline, where what is logical may depend on the platform in use. In MacPerl, \n always means \015. In DOSish perls, \n usually means \012, but when accessing a file in "text" mode, perl uses the :crlf layer that translates it to (or from) \015\012, depending on whether you're reading or writing.

like image 38
Eugene Yarmash Avatar answered Oct 23 '22 03:10

Eugene Yarmash


try this before while

binmode($in);
like image 28
Nahuel Fouilleul Avatar answered Oct 23 '22 01:10

Nahuel Fouilleul