chester has asked for the wisdom of the Perl Monks concerning the following question:
I was using File::Slurp and I noticed newlines were constantly being added to the end of every line after multiple reads/writes of the same file. The module appears (to me) to be buggy in Windows.
use strict; use warnings; use File::Slurp; use Data::Dump qw{dump}; undef $/; my $file_slurp = read_file('file'); open my $FILE, '<', 'file' or die $!; my $normal = <$FILE>; close $FILE; $normal eq $file_slurp ? print "Yep.\n" : print "Nope.\n"; dump($file_slurp); dump($normal);
file:
Line Line2 Line3
output:
Nope. "Line\r\nLine2\r\nLine3\r\n\r\n" "Line\nLine2\nLine3\n\n"
I undef $/ here because it's the only thing I could think to try; File::Slurp mentions using it in passing, but it doesn't help. Putting the "normal" version in its own block and using local on $/ has the same result. I haven't tested this in Linux, but the difference between line terminators in Windows and Linux (and everything else) strikes me as a possible source of error here.
Two questions:
1) Can anyone confirm this behavior? Is it normal? I know Perl6 will have slurp; is there some nuance to slurp which causes this to be expected behavior?
2) Is it worth bothering to use a module to slurp files (in Perl5)? It's as simple as local-izing $/ and reading into a scalar. If I'm really feeling lazy I can throw it into my own slurp sub. Will Perl6's slurp have any more to offer?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File::Slurp bug? Should I bother?
by itub (Priest) on Sep 07, 2005 at 15:37 UTC | |
|
Re: File::Slurp bug? Should I bother?
by ikegami (Patriarch) on Sep 07, 2005 at 15:48 UTC | |
|
Re: File::Slurp bug? Should I bother?
by jmcnamara (Monsignor) on Sep 07, 2005 at 15:50 UTC | |
by ikegami (Patriarch) on Sep 07, 2005 at 17:14 UTC | |
by jmcnamara (Monsignor) on Sep 07, 2005 at 18:24 UTC | |
|
Re: File::Slurp bug? Should I bother?
by chester (Hermit) on Sep 07, 2005 at 16:07 UTC | |
by ikegami (Patriarch) on Sep 07, 2005 at 17:02 UTC | |
by chester (Hermit) on Sep 07, 2005 at 17:40 UTC | |
by ikegami (Patriarch) on Sep 07, 2005 at 18:12 UTC | |
by chester (Hermit) on Sep 07, 2005 at 18:30 UTC |