in reply to Re: file reading issues
in thread file reading issues

I notice you have a different method for reading an entire file into memory then I am used to seeing. I was wondering if there is any reason you are aware of that makes your way better or worse than the way I use (or if they are just different (TIMTOWTDI)). If there is, I'd love to hear about it.

read IN, my $html, -s $file; # your code my $file = do{ local $/; <IN> }; # The way I usually use.

Other monks... can anybody tell me if there is an advantage in one way or the other?

Update The secode line of cone above is the way I am used to seeing... I forgot to complete the comment. My question regards the difference between using $/ as opposed to using read to slurp in an entire file.


They say that time changes things, but you actually have to change them yourself.

—Andy Warhol

Replies are listed 'Best First'.
Re^3: file reading issues
by kwaping (Priest) on Aug 03, 2005 at 19:36 UTC
    What is the way you're used to seeing? Maybe I am the one in need of enlightenment and your way is superior.

    To answer your question, there is no reason why I do it that way except that's the way I learned how to do it. Maybe there was a good reason to do it like that back then which has now been made moot by advances in Perl - I don't know.

      Sorry... apparently I forgot to type part of my question... Allow me to update.


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

        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

        Interesting. Well, I love to learn, and now I have. Thank you.


        They say that time changes things, but you actually have to change them yourself.

        —Andy Warhol

Re^3: file reading issues
by anonymized user 468275 (Curate) on Aug 04, 2005 at 16:13 UTC
    IMO, the difference will be what kind of buffered I/O gets performed. Fuller explanation: Re: Speed reading (files)

    One world, one people