Thanks for the update, that does help clarify your post.

After doing some research and giving the issue some thought, I'd say your way is probably more efficient in most cases. The reason is that it doesn't need to stat the file (-s $file in my example).

The only area I see in which my way would be superior is if you didn't want to read the entire file, but instead only X bytes from it. As you know, the desired byte count is the third parameter passed to the "read" function.

I haven't done any benchmarking, this is just my semi-educated guess.


Update: After running a benchmark, it appears my way is substantially faster! See below, the numbers are seconds taken to execute 1000 loops of the code. See Speed reading (files).
using read 0.780380964279175 0.774328947067261 0.926505088806152 0.798418998718262 0.798079967498779 0.841788053512573 traditional slurp 1.34281802177429 1.325679063797 1.28992795944214 1.29997110366821 1.28340101242065 1.28570604324341

In reply to Re^5: file reading issues by kwaping
in thread file reading issues by mojobozo

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.