开发者

perl read multiple file

in Perl,

How to read multiple开发者_开发百科 file at once,

am genrating report at the end of the month,

per day one log file will create,

i want to read the entire month files ,

my file somthing like this t.log.mmddyyy


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 
    ...
}


you can do a

  • readdir to get the list of files in the directory
  • parse each file name to make sure it matches your format
  • open and read each file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜