[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Syntax error in perl. Old list of files and mail

Posted on 2009-02-10
15
Medium Priority
?
643 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

0
Comment
Question by:tia_kamakshi
  • 9
  • 5
15 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 23600578
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
 
LVL 85

Expert Comment

by:ozo
ID: 23600596
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
 
LVL 85

Expert Comment

by:ozo
ID: 23600695
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 85

Expert Comment

by:ozo
ID: 23600717
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
 
LVL 85

Expert Comment

by:ozo
ID: 23600749
the variable $I_DONT_HAVE_SENDMAIL was never declared
0
 
LVL 5

Expert Comment

by:TomLun
ID: 23600808
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
 

Author Comment

by:tia_kamakshi
ID: 23601328
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
 
LVL 85

Expert Comment

by:ozo
ID: 23605644


> 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
 
LVL 85

Expert Comment

by:ozo
ID: 23605679
> Line 7: Either remove the , between strings
remove the quotes also, and insert spaces

> or (better) remove the qw.
agree
0
 

Author Comment

by:tia_kamakshi
ID: 23609463
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
 
LVL 85

Expert Comment

by:ozo
ID: 23609481
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
 

Author Comment

by:tia_kamakshi
ID: 23609574
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
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 23609666
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
 

Author Comment

by:tia_kamakshi
ID: 23609758

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
 

Author Closing Comment

by:tia_kamakshi
ID: 31545007
Thanks your solution works
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question