Hello monks,

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?


In reply to File::Slurp bug? Should I bother? by chester

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.