Rich Fernandez:
: Is there an advantage to using "read", as you did above, as opposed to 
: just saying:
: 
: while(<MOVIE>){print;}
: close MOVIE;
: 
: Or is it just a case of TMTOWTDI? 

It's more efficient to read() binary files than to loop through them
with <>.  "while (<...>)" is good for text files, where the concept of
"individual lines" has meaning. However, for binary files, it doesn't;
you just want (preferebly not tiny) chunks of bytes, so there's no need
to go scanning through looking for record separators (which could be
plentiful in a binary file).

It also removes the temptation to shorten the code to:

print <MOVIE>;

which (I learned the hard way) will first create a list of "lines" from
your binary file, and then print them. Lists can be horribly
inefficient with memory:  I was getting out-of-memory errors just
printing .5Mb files.

If all I want to do is print a file to STDOUT without munging it in any
way, I use read(), even if it's a text file.

-- tdk

Reply via email to