in reply to line ending troubles
#!/usr/bin/perl use strict; use warnings; use Perl6::Slurp; # generate test file "le.txt" my $win_line = "Windows\r\n"; my $unix_line = "Unix\n"; my $mac_line = "Mac\r"; open(my $fh, ">", "le.txt") or die "Failed file open: $!"; binmode($fh); print $fh $win_line; print $fh $unix_line; print $fh $mac_line; close($fh); # read file with slurp #my @lines = slurp("le.txt", {chomp => 1, irs => qr/(\r\n)|(\n)|(\r)/} +); my @lines = slurp("le.txt", {chomp => 1, irs => qr/\r\n|\n|\r/}); for my $line (@lines) { print $line . "\n"; }
I assume this means that the module makes use of the capture buffers internally, which your captures overwrite. Also notice I added an or die clause to your open statement, since that's usually a Good Thing(TM). For your pure regular expression solution, is there a reason you didn't just split on a regular expression, a la:
for my $line (split /\r\n?|\n/, $file_content) { print $line . "\n"; }
Update: I looked at my inbox, and decided I did feel like delving. Your issue is that capturing parentheses mean 'include my delimiter in the result set' (see split, or run the code split /(k)/, 'onektwokthree'). Since Perl6::Slurp uses split to process the results, it inserts your delimeters into the result stream. In fact, because Perl6::Slurp already uses delimiter capturing in the result (I don't see why, but see line 106), you end up with a real mess in the resulting split. The module then drops every other element of the array, which drops some of your results. The initial split results are:
@line = ("Windows", "\n", "", "\n", "", "Unix", "\n", "", "\n", "", "Mac", "\r", "", "\r", "");
The module was written by Damian Conway, who is much smarter than I am. Anyone know why he'd use parens in the split and then manually drop alternating terms? He used the delimiter capture to control the chomp behavior.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: line ending troubles
by Dirk80 (Pilgrim) on Dec 22, 2009 at 22:08 UTC | |
by kennethk (Abbot) on Dec 22, 2009 at 23:01 UTC | |
by Dirk80 (Pilgrim) on Dec 25, 2009 at 00:31 UTC | |
by kennethk (Abbot) on Dec 28, 2009 at 22:18 UTC |