tia_kamakshi
asked on
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/l ocal/appli c/cmc/admi n$ 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/l ocal/appli c/cmc/admi n$
I am adding code in code snippet part
Thanks for your co-operation
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/l
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/l
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();
}
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 !;
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 !;
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
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
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
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
the variable $I_DONT_HAVE_SENDMAIL was never declared
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","/hom e/hgftuiik /out","/ho me/abcdmcc /Registrat ionRequest ","/home/a bcdmcc/Mes sageReques t","/home/ abcdmcc/Br ochureRequ est","/hom e/abcdmcc/ EmailReque st");
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_pat h}}, $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();
}
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","/hom
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_pat
}
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();
}
ASKER
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
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
> 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?
> Line 7: Either remove the , between strings
remove the quotes also, and insert spaces
> or (better) remove the qw.
agree
remove the quotes also, and insert spaces
> or (better) remove the qw.
agree
ASKER
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
https://www.experts-exchange.com/questions/24111395/Finding-list-of-files-older-than-30-min.html?anchorAnswerId=23609427#a23609427
My error is
abcdmcc@tgfr-tstva2:/usr/l ocal/appli c/cmc/admi n$ 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/l ocal/appli c/cmc/admi n$
I am pasting my updated code
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
https://www.experts-exchange.com/questions/24111395/Finding-list-of-files-older-than-30-min.html?anchorAnswerId=23609427#a23609427
My error is
abcdmcc@tgfr-tstva2:/usr/l
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/l
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();
}
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 "::").
(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 "::").
ASKER
How should I fix this in my code.
Can you pls help me as I am new to perl
Thanks for your co-operation
Can you pls help me as I am new to perl
Thanks for your co-operation
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
https://www.experts-exchange.com/questions/24111395/Finding-list-of-files-older-than-30-min.html?anchorAnswerId=23609427#a23609427
Please guide
ASKER
Thanks your solution works
my @DIR_PATHS = ("/home/hgftuiik/in","/hom
or
my @DIR_PATHS = qw(/home/hgftuiik/in /home/hgftuiik/out /home/abcdmcc/Registration