in reply to Multiple file input into a perl script

It depends. How do you find all those files? If you have them in a text file, read them from that text file:

use strict; my $filename = 'my_textfile.txt'; open my $fh, $filename or die "Couldn't read '$filename': $!"; chomp @ARGV = <$fh>; print "Processing $_" for @ARGV;

If they all are below a certain directory, use File::Find:

use strict; use File::Find; my $directory = '/home/kelder/files'; find(sub { push @ARGV, $File::Find::name; }, $directory ); print "Processing $_" for @ARGV;

If you want to use a shell glob pattern, you can prevent the shell from expanding it and do the expansion in Perl:

use strict; use File::DosGlob qw(bsd_glob); # to get sane whitespace semantics my $pattern = '/home/kelder/files/*.txt'; @ARGV = glob $pattern; print "Processing $_" for @ARGV;

If you have the list in some other fashion, you'll have to tell us, but the basic pattern remains.

Replies are listed 'Best First'.
Re^2: Multiple file input into a perl script
by kelder (Novice) on Sep 30, 2008 at 19:01 UTC
    I thought about using the glob function, since all of my files are in the same directory, but because I'm new to perl I might be misuing the format: My Code:
    @files=<ABi1*>; foreach $file (@files) { while(<>) { Do my function; } if (eof($file)) { Do my end of file cleanup } }
    Does the way I set this up work, or am I completely screwing up the way you are supposed to use this function?

      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 };
        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.)
Re^2: Multiple file input into a perl script
by kelder (Novice) on Sep 30, 2008 at 19:53 UTC
    This worked out great:
    use File::DosGlob qw(bsd_glob); # to get sane whitespace semantics my $pattern = '/home/kelder/files/*.txt'; @ARGV = glob $pattern;
    Only there is a new problem; my output file list reads like this:
    Ai0.txt Ai1.txt Ai2.txt ... Ai10.txt Ai11.txt
    What ends up happening is that in my "results" file, where I print my counts, the order of the inputs is messed up.
    Output: File1 File10 File11 File2
    How do I fix this ordering? Can I input the files in the order they are in the directory with another method? Thanks for all of the help so far!!
        Sorting would be very useful, only I want to be able to sort the input files before I use their input- can I use the sort function on  @ARGV=glob $mypattern?