Perl New Line separator issue

2020-04-03 01:57发布

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.

标签: perl newline
3条回答
倾城 Initia
2楼-- · 2020-04-03 02:19

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.

查看更多
放我归山
3楼-- · 2020-04-03 02:27

try this before while

binmode($in);
查看更多
Evening l夕情丶
4楼-- · 2020-04-03 02:35

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.

查看更多
登录 后发表回答