开发者

Is it possible to read multiple files with a single filehandle in Perl?

开发者 https://www.devze.com 2022-12-24 06:18 出处:网络
I have a few log files like these: /var/log/pureftpd.log /var/log/pureftpd.log-20100328 /var/log/pureftpd.log-20100322

I have a few log files like these:

  • /var/log/pureftpd.log
  • /var/log/pureftpd.log-20100328
  • /var/log/pureftpd.log-20100322

Is it possi开发者_运维技巧ble to load all of them into a single filehandle or will I need to load each of them separately?


One ugly hack would be this:

local @ARGV = qw(
    /var/log/pureftpd.log 
    /var/log/pureftpd.log-20100328 
    /var/log/pureftpd.log-20100322
);

while(<>) {
    # do something with $_;
}


You could use pipes to virtually concat these files to a single one.


It's not terribly hard to do the same thing with a different filehandle for each file:

foreach my $file ( @ARGV )
    {
    open my($fh), '<', $file or do { warn '...'; next };
    while( <$fh> )
         {
         ...
         }
    }
0

精彩评论

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

关注公众号