2

I have a few log files like these:

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

Is it possible to load all of them into a single filehandle or will I need to load each of them separately?

3 Answers 3

5

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 $_;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Use the special ARGV filehandle to implicitly open on each file in @ARGV.
This is what I do. I wouldn't call it "ugly", but it's not beautiful either.
Well, the reason it's "ugly" is because it's fragile: any other part of your code (or someone else's) can break this code by munging @ARGV. For for a quick one-off script, it's fine.
1

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

1 Comment

Thanks, that was the keyword I was looking for.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.