in reply to Using a loop to process multiple files

Hi, I realize you're not a programmer, but I'm curious where you've picked up the use of <...> in array context as in:
@A = <DAT>;
It's not wrong, but I've seen it used quite a bit by new perl programmers, and I wonder why they prefer that to using the while (<DAT>) {...} construct. In fact, often I see <...> used in array context immediately followed by a foreach loop iterating over the array, and it makes me cringe.

Like I said, it's not a wrong practice. It can be more wasteful of memory than a while loop, but on today's hardware that's probably not a big concern. Is it just conceptually easier to think in terms of "read the data into an array and then iterate over the array" as opposed to "read in a line and process it"? In most other programming languages the while loop approach is the only way to process a file a line at a time, so I wonder why I see this practice so much.

Anyway, any insight into why you decided to use @A = <DAT>, where you've seen it or picked it up from would be helpful. Thanks!

Replies are listed 'Best First'.
Re^2: Using a loop to process multiple files
by dragonchild (Archbishop) on May 08, 2008 at 22:32 UTC
    A number of the non-O'Reilly books use it. I did tech-editing for one and tried my darndest to get it out of there, but the author wouldn't budge.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re^2: Using a loop to process multiple files
by Anonymous Monk on May 08, 2008 at 22:59 UTC
    Just to follow up...

    It's also easier for me to think of a (text) file as a single entity. Reading/Processing a file line by line is counterintuitive for me.

      That's an interesting comment, because the operating system doesn't think of a file as a collection of lines either. One of the motivations for making it easy to read files on a line-by-line or record-by-record basis in Perl was to match how people think, but that may rely on what people want to do with Perl more than any gestalt intuition about the nature of files.

        As I said, I am not a programmer, so the finer points of programming are lost on me. As an user however, and more as a social scientist, I have to deal with statistical data and/or text documents. For statistical data, especially time series data, I feel more comfortable in having the computer read/process a whole variable (all records) at once rather than piecemeal. For text documents, I also feel more comfortable in having the computer read the entire document at once and then process it according to my requirements. Bottom line however, I am open to any technique that gets the job done with minimum programming effort on my part. But that's just an user's perspective. I appreciate that a programmer's perspective may be very different.
Re^2: Using a loop to process multiple files
by wfsp (Abbot) on May 09, 2008 at 07:08 UTC
    In fact, often I see <...> used in array context immediately followed by a foreach loop iterating over the array, and it makes me cringe.
    Or immediately preceded by map?
    open my $fh, q{<}, q{stop_words.txt} or die qq{cant open stop: $!\n}; my %stop = map{chomp; $_ => undef} <$fh>;
    In fact I have a trivial wrapper MyIO script where I can
    my @array = $io->read_array(q{smallish_file});
    and later
    $io->write_array(q{tweaked_file}, \@array);
    There are a couple for strings too (and for returning a hash). If it isn't a 'big' file, say an Apache access log, I never bother with file handles. They are always an intermediate step I could do without.

    About the only file handle I use regularly is <DATA> because it is handy when posting on PerlMonks. :-)

Re^2: Using a loop to process multiple files
by Anonymous Monk on May 08, 2008 at 22:48 UTC
    I got it from "Perl for Dummies" -- if I remember correctly.

      If you have any desire to look at another book, I can't sing the praises of Learning Perl highly enough. Of course some people think this book requires a background in programming to really get.

      You can find the full text of Beginning Perl online. This book is aimed at non-programmers.

      Many people recommend Elements of Programming with Perl to beginners as well.

      Happy travels!


      TGI says moo