We help IT Professionals succeed at work.

Syntax error in perl. Old list of files and mail

tia_kamakshi
tia_kamakshi asked
on
Medium Priority
686 Views
Last Modified: 2012-05-06
Hi,

I am new to perl.

I need the list of files in the directories, which are older than 1 hour. I get these list by mail

I have code to do it. I am testing the code

I am getting error below

Can anyone help me in fixing that

abcdmcc@pbms-tstva2:/usr/local/applic/cmc/admin$ perl ListAgedFiles.pl
Possible attempt to separate words with commas at ListAgedFiles.pl line 7.
Possible attempt to separate words with commas at ListAgedFiles.pl line 7.
Possible attempt to separate words with commas at ListAgedFiles.pl line 7.
Possible attempt to separate words with commas at ListAgedFiles.pl line 7.
Possible attempt to separate words with commas at ListAgedFiles.pl line 7.
syntax error at ListAgedFiles.pl line 27, near ");"
Global symbol "$I_DONT_HAVE_SENDMAIL" requires explicit package name at ListAgedFiles.pl line 31.
syntax error at ListAgedFiles.pl line 35, near "}"
Execution of ListAgedFiles.pl aborted due to compilation errors.
abcdmcc@pbms-tstva2:/usr/local/applic/cmc/admin$

I am adding code in code snippet part

Thanks for your co-operation
#!perl
use strict;
use warnings;
 
use MIME::Lite;
 
my @DIR_PATHS = qw("/home/hgftuiik/in","/home/hgftuiik/out","/home/abcdmcc/RegistrationRequest","/home/abcdmcc/MessageRequest","/home/abcdmcc/BrochureRequest","/home/abcdmcc/EmailRequest");
 
 
my %old_files;
foreach my $dir_path ( @DIR_PATHS ) {
    opendir my $DIR, $dir_path or die $!;
 
    while (my $file_path = readdir(DIR)){
        next if not -f $file_path;         # return if it isn't a file
        push(@{$old_files{$dir_path}}, $file_path);
    }
    closedir($DIR);
}
 
 
while ( my ($dir_path, $files) = each %old_files ) {
    my $msg = MIME::Lite->new(
        From => 'youremail@address.com',
        To => 'youremail@address.com',
        Subject => "old files in $dir_path",
        Data => join("\n", @$files);
    );
 
    # send the email
    if ($I_DONT_HAVE_SENDMAIL) {
       MIME::Lite->send('smtp', 'mail.example.com', Timeout=>60);
    }
    $msg->send();
}

Open in new window

Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
yoy probably mean either
 
my @DIR_PATHS = ("/home/hgftuiik/in","/home/hgftuiik/out","/home/abcdmcc/RegistrationRequest","/home/abcdmcc/MessageRequest","/home/abcdmcc/BrochureRequest","/home/abcdmcc/EmailRequest");
 
or

my @DIR_PATHS = qw(/home/hgftuiik/in /home/hgftuiik/out  /home/abcdmcc/RegistrationRequest /home/abcdmcc/MessageRequest  /home/abcdmcc/BrochureRequest  /home/abcdmcc/EmailRequest );
 
 
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
perl -MDiagnostics ListAgedFiles.pl
Possible attempt to separate words with commas at ListAgedFiles.pl line 7 (#1)
    (W qw) qw() lists contain items separated by whitespace; therefore
    commas aren't needed to separate the items.  (You may have used
    different delimiters than the parentheses shown here; braces are also
    frequently used.)
   
    You probably wrote something like this:
   
        qw! a, b, c !;
   
    which puts literal commas into some of the list items.  Write it without
    commas if you don't want them to appear in your data:
   
        qw! a b c !;
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
the ; on line 27 does not belong there

perl  -Mdiagnostics  ListAgedFiles.pl
   
syntax error at ListAgedFiles.pl line 27, near ");"
Global symbol "$I_DONT_HAVE_SENDMAIL" requires explicit package name at ListAgedFiles.pl line 31.
Execution of ListAgedFiles.pl aborted due to compilation errors (#2)
    (F) Probably means you had a syntax error.  Common reasons include:
   
        A keyword is misspelled.
        A semicolon is missing.
        A comma is missing.
        An opening or closing parenthesis is missing.
        An opening or closing brace is missing.
        A closing quote is missing.
   
    Often there will be another error message associated with the syntax
    error giving more information.  (Sometimes it helps to turn on -w.)
    The error message itself often tells you where it was in the line when
    it decided to give up.  Sometimes the actual error is several tokens
    before this, because Perl is good at understanding random input.
    Occasionally the line number may be misleading, and once in a blue moon
    the only way to figure out what's triggering the error is to call
    perl -c repeatedly, chopping away half the program each time to see
    if the error went away.  Sort of the cybernetic version of S<20
    questions>.
   
Uncaught exception from user code:
        syntax error at ListAgedFiles.pl line 27, near ");"
Global symbol "$I_DONT_HAVE_SENDMAIL" requires explicit package name at ListAgedFiles.pl line 31.
Execution of ListAgedFiles.pl aborted due to compilation errors.
 at ListAgedFiles.pl line 35
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
perl  -Mdiagnostics  ListAgedFiles.pl    
Global symbol "$I_DONT_HAVE_SENDMAIL" requires explicit package name at ListAgedFiles.pl line 31.
Execution of ListAgedFiles.pl aborted due to compilation errors (#1)
    (F) You've said "use strict" or "use strict vars", which indicates
    that all variables must either be lexically scoped (using "my" or "state"),
    declared beforehand using "our", or explicitly qualified to say
    which package the global variable is in (using "::").
   
Uncaught exception from user code:
        Global symbol "$I_DONT_HAVE_SENDMAIL" requires explicit package name at ListAgedFiles.pl line 31.
Execution of ListAgedFiles.pl aborted due to compilation errors.
 at ListAgedFiles.pl line 35
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
the variable $I_DONT_HAVE_SENDMAIL was never declared

Commented:
Line 7: Either remove the , between strings or (better) remove the qw.
Line 27: Remove the ; - not end of a statement. It's just the end of a literal hash
Line 31: Since you're using 'strict' you need to declare the variable.

Try this one:


#!perl
use strict;
use warnings;
 
use MIME::Lite;

my $I_DONT_HAVE_SENDMAIL=0;

my @DIR_PATHS = ("/home/hgftuiik/in","/home/hgftuiik/out","/home/abcdmcc/RegistrationRequest","/home/abcdmcc/MessageRequest","/home/abcdmcc/BrochureRequest","/home/abcdmcc/EmailRequest");
 
 
my %old_files;
foreach my $dir_path ( @DIR_PATHS ) {
    opendir my $DIR, $dir_path or die $!;
 
    while (my $file_path = readdir(DIR)){
        next if not -f $file_path;         # return if it isn't a file
        push(@{$old_files{$dir_path}}, $file_path);
    }
    closedir($DIR);
}
 
 
while ( my ($dir_path, $files) = each %old_files ) {
    my $msg = MIME::Lite->new(
        From => 'youremail@address.com',
        To => 'youremail@address.com',
        Subject => "old files in $dir_path",
        Data => join("\n", @$files)
    );
 
    # send the email
    if ($I_DONT_HAVE_SENDMAIL) {
       MIME::Lite->send('smtp', 'mail.example.com', Timeout=>60);
    }
    $msg->send();
}

Author

Commented:
Many Thanks,

I am still getting below errors

syntax error at ListAgedFiles.pl line 27, near ");"
Global symbol "$I_DONT_HAVE_SENDMAIL" requires explicit package name at ListAgedFiles.pl line 31.
syntax error at ListAgedFiles.pl line 35, near "}"
Execution of ListAgedFiles.pl aborted due to compilation errors.

Please guide

Thanks again
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:


> syntax error at ListAgedFiles.pl line 27, near ");"

Did you remove the ; on line 27?

where is $I_DONT_HAVE_SENDMAIL supposed to come from?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
> Line 7: Either remove the , between strings
remove the quotes also, and insert spaces

> or (better) remove the qw.
agree

Author

Commented:
Thanks, now line no 27 error has gone

But I still get error at line 31 as pasted below

I don't know from where "$I_DONT_HAVE_SENDMAIL" has come

I am proposed this solution from the URL

http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_24111395.html#a23609427

My error is

abcdmcc@tgfr-tstva2:/usr/local/applic/cmc/admin$ perl ListAgedFiles.pl
Global symbol "$I_DONT_HAVE_SENDMAIL" requires explicit package name at ListAgedFiles.pl line 31.
Execution of ListAgedFiles.pl aborted due to compilation errors.
abcdmcc@tgfr-tstva2:/usr/local/applic/cmc/admin$

I am pasting my updated code
#!perl
use strict;
use warnings;
 
use MIME::Lite;
 
my @DIR_PATHS = qw(/home/defg/in /home/defg/out /home/abcdmcc/RegistrationRequest /home/abcdmcc/MessageRequest /home/abcdmcc/BrochureRequest /home/abcdmcc/EmailRequest);
 
 
my %old_files;
foreach my $dir_path ( @DIR_PATHS ) {
    opendir my $DIR, $dir_path or die $!;
 
    while (my $file_path = readdir(DIR)){
        next if not -f $file_path;         # return if it isn't a file
        push(@{$old_files{$dir_path}}, $file_path);
    }
    closedir($DIR);
}
 
 
while ( my ($dir_path, $files) = each %old_files ) {
    my $msg = MIME::Lite->new(
        From => 'yourmail@mail.com',
        To => 'yourmail@mail.com',
        Subject => "old files in $dir_path",
        Data => join("\n", @$files)
    );
 
    # send the email
    if ($I_DONT_HAVE_SENDMAIL) {
       MIME::Lite->send('smtp', 'mail.example.com', Timeout=>60);
    }
    $msg->send();
}

Open in new window

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
you did not declare $I_DONT_HAVE_SENDMAIL


    (F) You've said "use strict" or "use strict vars", which indicates
    that all variables must either be lexically scoped (using "my" or "state"),
    declared beforehand using "our", or explicitly qualified to say
    which package the global variable is in (using "::").

Author

Commented:
How should I fix this in my code.

Can you pls help me as I am new to perl

Thanks for your co-operation
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
as the Diagnostics module suggests, adding
    my($I_DONT_HAVE_SENDMAIL);
would be one way to eliminate the error,

however if you don't set it to anything meaningful, then testing it may not do anything sensible either.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:

Thanks for your quick response

I have removed

if ($I_DONT_HAVE_SENDMAIL) {
    MIME::Lite->send('smtp', 'mail.example.com', Timeout=>60);
}

by

MIME::Lite->send('smtp', 'mail.example.com', Timeout=>60);

Now I get the error

perl ListAgedFiles.pl
Name "main::DIR" used only once: possible typo at ListAgedFiles.pl line 14.


See URL
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_24111395.html#a23609427

Please guide

Author

Commented:
Thanks your solution works
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.