Syntax error in perl. Old list of files and mail

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

tia_kamakshiAsked:
Who is Participating?
 
ozoCommented:
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.
0
 
ozoCommented:
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 );
 
 
0
 
ozoCommented:
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 !;
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
ozoCommented:
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
0
 
ozoCommented:
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
0
 
ozoCommented:
the variable $I_DONT_HAVE_SENDMAIL was never declared
0
 
TomLunCommented:
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();
}
0
 
tia_kamakshiAuthor 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
0
 
ozoCommented:


> 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?
0
 
ozoCommented:
> Line 7: Either remove the , between strings
remove the quotes also, and insert spaces

> or (better) remove the qw.
agree
0
 
tia_kamakshiAuthor 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

0
 
ozoCommented:
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 "::").
0
 
tia_kamakshiAuthor 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
0
 
tia_kamakshiAuthor 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
0
 
tia_kamakshiAuthor Commented:
Thanks your solution works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.