in reply to line ending troubles
Yet another way would be to write a custom PerlIO layer (similar in spirit to :crlf, but for reading only), e.g. using PerlIO::via.
In its most simple form it could look something like:
package PerlIO::via::AnyCRLF; # save as PerlIO/via/AnyCRLF.pm sub PUSHED { my ($class) = @_; my $dummy; return bless \$dummy, $class; } sub FILL { my ($self, $fh) = @_; my $len = read $fh, my $buf, 4096; if (defined $buf) { $buf =~ s/\r\n/\n/g; $buf =~ s/\r/\n/g; } return $len > 0 ? $buf : undef; } 1;
Sample usage:
#!/usr/bin/perl use PerlIO::via::AnyCRLF; open my $f, "<:via(AnyCRLF)", "le.txt" or die $!; print while <$f>;
Handling the corner case (when \r\n gets split such that \r is in one buffer read, and \n in the next) is left as an exercise for the reader ;) — A quick fix could be to delegate the \r\n to \n translation to the regular :crlf layer (i.e. "<:crlf:via(AnyCRLF)"), and only do the \r to \n translation in this layer...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: line ending troubles
by Dirk80 (Pilgrim) on Dec 22, 2009 at 22:23 UTC | |
by almut (Canon) on Dec 22, 2009 at 22:55 UTC | |
by Dirk80 (Pilgrim) on Dec 26, 2009 at 16:22 UTC | |
by almut (Canon) on Dec 27, 2009 at 23:10 UTC |