Tom Smith wrote:
> I'm writing a Perl script to parse 31 maillog files. The files are named
> maillog, maillog.1, and so on up to 31. This is the default logrotate
> scheme for naming rotated logs.
> 
> My current order for processing these files is this:
> 
> 1) Open the directory.
> 2) List maillog* files in the directory and assign them to @maillog.
> 3) Open each file in @maillog and search it for two specific strings.
> Assign matching strings to @parsedmail.
> 4) Print @parsedmail.
> 
> I've tested the search string on a single maillog file and it works as
> expected, but putting this script together doesn't work correctly. I
> think it has something to do with the way I'm trying to pass the array
> to open(FILE...). The script is listed below.
> 
> (Note that the maillogs in questions were moved from our mail server to
> a Windows machine for parsing. Also, the script is being run from the
> directory that contains the maillogs--hence the "." for the DIR to open.
> And "print @maillog" is hashed out as it tested good--that is, is prints
> the array and contains the file names I expected to be there.)
> 
> #!C:\Perl\bin\perl.exe
> 
> use strict;
> use warnings;
> use Carp;
> 
> my @maillog;
> opendir(DIR, '.') or croak "Can't open .$!";
> while ($_ = readdir(DIR)) {
>    if($_ =~ /^maillog.*$/) {
>        push(@maillog,$_);
>    }
> }
> closedir DIR;
> #print @maillog;
> 
> my @parsedmail;
> while ($_ = @maillog) {
>    open(FILE,'<$_') or croak "Can't open $_$!";
>    while($_ = <FILE>) {
>        if($_ =~ /.*imapd.*Log.*user.*$/ || /.*pop3d.*Log.*user.*$/) {
>            push(@parsedmail,$_);
>        }
>    }
>    close(FILE);
> }
> print @parsedmail;
> 
> 
> The error I get after executing this script is: "Can't open 32No such
> file or directory at H:\User Files\Mail Logs\parse.pl line 31" Maybe a
> coincidence, but "32" is the number of maillog files in this directory.

No it is not a coincidence.  At the line:

while ($_ = @maillog) {

When you use an array in a scalar context (like you are here) it evaluates to
the number of elements in the array so the number of elements is assigned to
the scalar variable.


> Can anyone offer any advice for correcting this?

You could use some of Perl's idioms to do what you want:

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

@ARGV = glob 'maillog*' or die "No maillog files found.\n";

while ( <> ) {
    print if /(?:imapd|pop3d).*?Log.*?user/;
    }

__END__




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to