Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Multiple file input into a perl script

by Corion (Patriarch)
on Sep 30, 2008 at 19:47 UTC ( [id://714622]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Multiple file input into a perl script
in thread Multiple file input into a perl script

The magic diamond-operator <> only works if you stuff the filenames into @ARGV. But you surely have tried that yourself and merely forgot to tell me that you found your code didn't work the way you wrote it.

I recommend you read up on open to learn how to open and read a single file and process that, and then proceed to do that in the loop:

use strict; use File::DosGlob qw(bsd_glob); my @files = glob 'ABi1*'; foreach my $file (@files) { open my $fh, '<', $file or die "Couldn't read '$file': $!"; while (<$fh>) { ... do your function }; # EOF, do end of file cleanup };

Replies are listed 'Best First'.
Re^4: Multiple file input into a perl script
by broomduster (Priest) on Sep 30, 2008 at 22:11 UTC
    The magic diamond-operator <> only works if you stuff the filenames into @ARGV.
    My reading of I/O Operators has me believing that using the diamond operator for globbing works as the OP seems to expect. Here's what I get with a simple test:
    -> ls file1.txt file2.txt file3.txt -> perl -le '@files = <file*>; print @files;' file1.txt file2.txt file3.txt
    I may have missed something, but it looks to me that kelder's expectation of how globbing works with <> is right. Is there a platform difference to worry about? (OP is on a Mac, above test runs the same on a Mac and under NetBSD.)
      That part works. The empty diamond, used to read the contents of all files (or STDIN) is what doesn't work without setting up @ARGV. That dual use is why I prefer an explicit glob() over the diamond...
        That dual use is why I prefer an explicit glob() over the diamond..
        And so do I ;-)... In fact, just last week at $DAYJOB, I had an interesting "discussion" with $COWORKER who is relatively new to Perl and insisted that
        • while ( <> )
        • while ( <file*> )
        • while ( <@ARGV> )
        all do the same thing. When $COWORKER was confronted with examples of the differences, the "problem" morphed first into "a bug in Perl", then to "incorrect documentation", then to "should do the same thing" (I guess that's progress :-) ). All of which is a long (and hopefully slightly humorous) explanation of why I should have caught what you were saying the first time.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://714622]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-19 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found