Date: 2012may18
Update: 2025oct22
Language: perl
Q. perl: How to break up a string into lines in perl?
A. Here you go:
foreach $line (split(/\r\n|\n/, $contents)) {
print "$line\n";
}
This handles lines that end in CR-LF (like HTTP and Windows) or
LF (like Linux and iOS); the vast majority of situations.
It also preserves blank lines.
If you want to read a file into lines don't to the above, to do this:
open(FILE, 'myfile.txt');
while ($line = <FILE>) {
chop($line);
print "$line\n";
}
close(FILE);