Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've put together a script to find a certain word if it occurs in a file directory. All together the script is doing what I hoped it would but I think I've come a cropper on the difference between array and list. I'm trying to put the $File::Find::name into a list so that one email is sent to the recipient rather than an email per time the pattern occurs (which is what is happening).
#!c:\perl\bin\perl.exe use strict; use warnings; use File::Find; use Net::SMTP; my $mailserver_url = "foo"; my $from = 'foo@bar.com'; my $to = 'foo@bar.com'; my $subject = "CONNREFUSED lists"; my @result =(); undef $/; find( sub { return if ($_ =~ /^\./); return unless ($_ =~ /\.autodel/i); stat $File::Find::name; return if -d; return unless -r; open(FILE, "< $File::Find::name") or return; my $string = <FILE>; close (FILE); return unless ($string =~ /\bCONNREFUSED\b/i); my $smtp = Net::SMTP->new(Host => 'foo'); #Sending the message $smtp->mail( $from ); $smtp->to( $to ); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); # done with header $smtp->datasend("$results\n"); $smtp->dataend(); $smtp->quit(); # all done. message sent. }, 'X:\\baz\\MAIN');
I did try setting up @results and using push $File::Find::name into it but it has failed to collect all the data together in one, rather it outputs 1, hence my wondering about the arrays and lists. I'd be grateful for some enlightenment on how to do this most effectively.

Replies are listed 'Best First'.
Re: Collecting a list of filenames together
by wfsp (Abbot) on Dec 11, 2007 at 14:53 UTC
    You declare
    my @result = ();
    but don't use it.

    I would take all the smtp stuff out of the sub and replace it with

    push @result, $string;
    and then at the end of the script send the results (perhaps with some prettifying).
    my $email_body = join qq{\n******\n}, @results;
    update: changed the last sentence into something closer to English.
Re: Collecting a list of filenames together
by holli (Abbot) on Dec 11, 2007 at 14:52 UTC
    Well, you have your mailing code within the callback that gets called for every item, so the behaviour you describe is exactly what one would expect. You must put this code after the call to find().


    holli, /regexed monk/
Re: Collecting a list of filenames together
by osunderdog (Deacon) on Dec 11, 2007 at 16:40 UTC