CR vs LF perl parsing

I have a perl script that parses a text file and splits it into a string into an array. It works fine when each line ends with LF, but when they end with CR my script, it is not processed correctly. How can I change this line to fix this.

my @allLines = split(/^/, $entireFile);

edit: There is a mixture of lines in my file from ending with LF or ending with CR, it just destroys all lines when it ends in CR

+5
source share
4 answers

Perl can handle both ends of CRLF and LF with an embedded :crlf PerlIO layer :

open(my $in, '<:crlf', $filename);

CRLF LF LF . CR - . , CR-, $/ "\r" ( CR LF).

( ), PerlIO::eol. :

open(my $in, '<:raw:eol(LF)', $filename);

CR, CRLF LF LF .

- $/ undef, slurp. /\r\n?|\n/. , , .

+11

, , :

 use v5.10;

 $entireFile =~ s/\R/\n/g;

, :

 open my $fh, '<', \ $entireFile;
 my @lines = <$fh>;
 close $fh;

, cjm.

+5

, split, :

my @allLines = split(/\r\n|\r|\n/, $entireFile);
+1

, <>, $/ \r.

$/ " ". . perldoc perlvar.

, end-of-line - .

0

All Articles