I have a perl script which parses a text file and breaks it up per line into an array.
It works fine when each line are terminated by LF but when they terminate by CR my script is not handling properly.
How can I modify this line to fix this
my @allLines = split(/^/, $entireFile);
edit:
My file has a mixture of lines with either
ending LF or ending CR it just collapses all lines when its ending in CR
Perl can handle both CRLF and LF line-endings with the built-in
:crlfPerlIO layer:will automatically convert CRLF line endings to LF, and leave LF line endings unchanged. But CR-only files are the odd-man out. If you know that the file uses CR-only, then you can set $/ to
"\r"and it will read line-by-line (but it won’t change the CR to a LF).If you have to deal with files of unknown line endings (or even mixed line endings in a single file), you might want to install the PerlIO::eol module. Then you can say:
and it will automatically convert CR, CRLF, or LF line endings into LF as you read the file.
Another option is to set
$/toundef, which will read the entire file in one slurp. Then split it on/\r\n?|\n/. But that assumes that the file is small enough to fit in memory.