[Solved] perl read multiple file


The glob function will allow you to retrieve a list of files names that match a certain pattern. If you load that list into @ARGV, then you can process all the files–even in order with a single loop:

use strict;
use warnings;
use Getopt::Long;

sub usage ($) { 
    my $msg = shift;
    die <<"END_MSG";
*** $msg
Usage: $0 --month=nn --year=nn PATH

END_MSG
}

GetOptions( 'month=i' => \my $month, 'year=i'  => \my $year );

usage "Month not specified!" unless $month;
usage "Year not specified!"  unless $year;
usage "Invalid month specified: $month" unless $month > 0 and $month < 13;
usage "Invalid year specified: $year"   unless $year  > 0;

my $directory_path = shift;
die "'$directory_path' does not exist!" unless -d $directory_path;

@ARGV = sort glob( sprintf( "$directory_path/t.log.%02d??%02d", $month, $year ));
while ( <> ) { # process all files 
    ...
}

solved perl read multiple file