Reputation: 25439
I'm maintaining a Perl script (Perl 5.10 on Linux) which needs to process a file line-by-line while being as flexible as possible regarding line separator characters. Any sequence of newlines and/or carriage return characters should mark the end of a line. Blank lines in the file aren't significant. For example, all of these should yield two lines:
FOO\nBAR FOO\rBAR
FOO\r\nBAR FOO\n\rBAR
FOO\r\n\r\r\r\n\n\nBAR
It doesn't look like it's possible to get this behavior through PerlIO
or by setting $/
. The files aren't large, so I suppose I could just read the whole file into memory and then split it with a regular expression. Is there are more clever way to do this in Perl?
Upvotes: 0
Views: 178
Reputation: 35208
Just slurp the file and use split
:
use strict;
use warnings;
use autodie;
use Data::Dump;
my @data = (
"FOO\nBAR",
"FOO\rBAR",
"FOO\r\nBAR",
"FOO\n\rBAR",
"FOO\r\n\r\r\r\n\n\nBAR",
);
for my $filedata (@data) {
dd $filedata;
open my $fh, "<", \"$filedata";
local $/;
for my $line (split /[\n\r]+/, <$fh>) {
print " $line\n";
}
}
Outputs:
"FOO\nBAR"
FOO
BAR
"FOO\rBAR"
FOO
BAR
"FOO\r\nBAR"
FOO
BAR
"FOO\n\rBAR"
FOO
BAR
"FOO\r\n\r\r\r\n\n\nBAR"
FOO
BAR
Upvotes: 3