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

Thanks to everyone who has helped me with this so far.

I want to search for a file named weekly in a series of sub-directories. If the file exists I want to gzip two log files and email them to someone.

The file glob works now (thanks Hofmator and others!) but the $a variable doesn't appear to be being processed in the $msg->build section of the code that creates the email messages.

I get this error message when I run the script.

/home/www/abc/logs gzip: /access_log: No such file or directory gzip: /error_log: No such file or directory /home/www/def/logs gzip: /access_log: No such file or directory gzip: /error_log: No such file or directory

What is the problem? Thanks in advance.

#!/usr/bin/perl -w use strict; use diagnostics; use MIME::Lite; #use a file glob in a for loop to search for marker file "weekly" # added braces and changed variable name for my $a (</home/www/*/logs/weekly>) { substr($a,-7)=" "; print $a; #build a mail message with the access_log & error log files gzipp'ed #and attached my $msg = MIME::Lite->build( From => 'root@abc.xyz.com', To => 'john.doe@joesoap.org', Subject => "gzipp'ed log file", Type =>'x-gzip', Path =>"/usr/bin/gzip < $a/access_log |", ReadNow => 1, Filename =>"access_log.tgz"); $msg->send; my $msg2 = MIME::Lite->build( From => 'root@abc.xyz.com', To => 'john.doe@joesoap.org', Subject => "gzipp'ed log file", Type =>'x-gzip', Path =>"/usr/bin/gzip < $a/error_log |", ReadNow => 1, Filename =>"error_log.tgz"); $msg2->send; }

Edit Masem 2001-08-21 - Code tags added, extra CRs removed

Replies are listed 'Best First'.
Re: Glob problem
by Hofmator (Curate) on Aug 21, 2001 at 15:32 UTC

    I think your problem lies here:

    for my $a (</home/www/*/logs/weekly>) { substr($a,-7)=" "; # <<< here
    You are trying to get rid of the last part of the path, I think your log files are stored in /home/www/*/logs/... But you are introducing an extra space by your substr command, it should be either
    substr($a,-7)= ""; # empty string! # or let the os handle the path $a .= '/..';

    Another thing, could you make sure next time that your code does not contain so many newlines, that makes it horribly hard to read, imho. Thanks.

    -- Hofmator

      Many thanks.

      Adding substr($a,-7)= ""; # empty string sorted things out

      Apologies for all the newlines. I'lll try and sort that out in future postings.

Re: Glob problem
by Zaxo (Archbishop) on Aug 21, 2001 at 15:22 UTC

    Does it make a difference if you rename $a to $c? $a is a special variable reserved for the use of sort and glob may invoke it.

    After Compline,
    Zaxo

      That should only be a problem when you are using a global $a, not a my variable, shouldn't it?? Nevertheless, in general a good advice to keep as far away as possible from $a and $b - however tempting they may be :)

      -- Hofmator

      Sadly not, but thanks for the suggestion.

      Wylie