Help is needed. I've encountered errors as follows
Syntax error in file /home/common/bin/NEL_MailHouseKeeper.pl at line 3, next 2 tokens "my $PATH "
Syntax error in file /home/common/bin/NEL_MailHouseKeeper.pl at line 122, next 2 tokens "my $FLAG_PRINT "
Excution of /home/common/bin/NEL_MailHouseKeeper.pl aborted due to compilation errors.

#!/usr/contrib/bin/perl

my $PATH = "/var/mail";
my $SHOWPATH = "/usr/bin/pwd";
my $RENAME = "/usr/bin/mv";
my $SWITCHPATH = "/usr/bin/cd";
my $COPY = "/usr/bin/cp";
my $DELETE = "/usr/bin/rm";
my $GZIP = "/usr/contrib/bin/gzip";
my $DATE = `/usr/bin/date '+%d_%b_%Y'`;
my $SENDMAIL = "/sbin/init.d/sendmail";
my $SLEEP = "/usr/bin/sleep";
my $MAILER = "/usr/bin/mailx";
my $EMAIL_SUBJECT = "";
my $EMAIL_C755 = "c755mnt";
my $EMAIL_PASS = "/home/common/email/EMAIL_MAIL_HOUSEKEEPER_PASS";
my $EMAIL_FAIL = "/home/common/email/EMAIL_MAIL_HOUSEKEEPER_FAIL"; 
my $EMAIL_ABORT = "/home/common/email/EMAIL_MAIL_HOUSEKEEPER_ABORT";

my $RETURN = 0;
my $CHECKBIT => 0;
my $BASEFILE = $ARGV[0];
my $DUPFILE = $ARGV[0]."_Copy";
my $TEMPFILE = $BASEFILE."_".${DATE};
my $TEMPFILE2 = $BASEFILE."_tmp";
my $LINE_BUFFER = "";
my $BUFFER = "";
my $TOTAL_MAILS = 0;
my $MAILS_TO_ARCHIVE = 0;
my $THRESHOLD = 10;
my $COUNTER = 0;


        #(0) <<START>>
        my $date = `/usr/bin/date`;
        print "Starting NEL_MAILHOUSEKEEPER script at $date \n";

        #(1) Stop sendmail
        $RETURN = system "$SENDMAIL stop";
        if ($RETURN == $CHECKBIT)
        {
            print "STEP 1: sendmail is stopped successfully. \n";
        }
        else
        {
            print "STEP 1: sendmail cannot be stopped ! \n";
            print "The task is aborted ! \n";
            $date = `/usr/bin/date`;
            print "Exiting at $date !!! \n";
            exit;
        }

        #(2) Goto working directory
        $RETURN = system "$SWITCHPATH $PATH";
        if ($RETURN == $CHECKBIT)
        {
            print "STEP 2: Path is switched to `$SHOWPATH` successfully. \n";
        }
        else
        {
            print "STEP 2: Could not switch to $PATH ! \n";
        }

        #(3) Duplicate original file for processing
        $RETURN = system "$COPY -p $BASEFILE $DUPFILE";
        if ($RETURN == $CHECKBIT)
        {
            print "STEP 3: The original mail file is duplicated successfully for processing. \n";
        }
        else
        {
            print "STEP 3: Duplication of original mail file fails ! \n";
            print "The task is aborted ! \n";

            #Start sendmail
            $RETURN = system "$SENDMAIL start";
                        system ("$SLEEP 1");
                        if ($RETURN == $CHECKBIT)
                        {
                                print "sendmail is started successfully. \n";
                $date = `/usr/bin/date`; 
                print "Exiting at $date !!! \n";
                exit;
            }
            else
            {
                print "sendmail cannot be started ! ISCS Maintenance needs to manually start sendmail IMMEDIATELY ! \n";
                exit;
            }
        }

            #Count the total number of emails first
        open(FILE, "<$DUPFILE");
        while ($LINE_BUFFER = <FILE>)
        {
            if ($LINE_BUFFER =~ m/^From /)
                    {
                            $TOTAL_MAILS++;
                    }
        }
        close(FILE);
        print "Total mails found in $BASEFILE -> $TOTAL_MAILS\n";

        #(4) Begin Processing the mails
        print "STEP 4: Processing the mails now......\n";

        if ($TOTAL_MAILS > $THRESHOLD)
        {
            #Processing the old emails into 1 mail file and leaving last 10 emails into original mail file 

            $MAILS_TO_ARCHIVE = $TOTAL_MAILS - $THRESHOLD;
            print "Total old mails to archive -> $MAILS_TO_ARCHIVE\n";

            open(FILE1, "<$DUPFILE");
            open(FILE2, ">$TEMPFILE");
            open(FILE3, ">$TEMPFILE2");

            while ($BUFFER = <FILE1>)
            {
                if ($BUFFER =~ m/^From /)
                {
                    my ${FLAG_PRINT} = 1;
                    $COUNTER++;
                }

                if (($FLAG_PRINT = 1) && ($COUNTER <= $MAILS_TO_ARCHIVE))
                {
                    print FILE2 $BUFFER;
                }
                elsif (($FLAG_PRINT = 1) && ($COUNTER > $MAILS_TO_ARCHIVE) && ($COUNTER <= $TOTAL_MAILS))
                {
                    print FILE3 $BUFFER;
                }
            }
            close(FILE1);
            close(FILE2);
            close(FILE3);

            print "Processing of the mails is completed......\n";
        }
        else
        {
            #Remove the Duplicate file
            $RETURN = system "$DELETE $DUPFILE";
            if ($RETURN == $CHECKBIT)
            {
                print "Duplicate mail file is deleted successfully \n";
            }
            else
            {
                print "$DUPFILE cannot be deleted ! ISCS Maintenance needs to delete it manually ! \n";
            }

            if ($TOTAL_MAILS < $THRESHOLD)
            {
                print "Total mails found are less than the threshold mails (10) ! \n";
            }
            elsif ($TOTAL_MAILS == $THRESHOLD)
            {
                print "Total mails found are same as the threshold mails (10) ! \n";
            }   
            #Start sendmail
            $RETURN = system "$SENDMAIL start";
            system ("$SLEEP 1");
            if ($RETURN == $CHECKBIT)
            {
                print "sendmail is started successfully. \n";

                #Send email informing maintenance team that task is aborted
                $EMAIL_SUBJECT = '"AUTOMATION TASK: NEL_MailHouseKeeper.pl script run result -> ABORT"';
                system ("$MAILER -s $EMAIL_SUBJECT $EMAIL_C755 < $EMAIL_ABORT");

                $date = `/usr/bin/date`;
                print "Exiting now at $date ......\n";
                print "++++++++++++++++++++++++++++++++++++++++++++++\n";
                exit;
            }
            else
            {
                print "sendmail cannot be started ! ISCS maintenance needs to manually start sendmail IMMEDIATELY ! \n";
                exit;
            }
        }

        #(5) Archive the old emails in 1 mail file (e.g. nelprofile-fwc_<date>.gz)
        $RETURN = system "$GZIP -9 $TEMPFILE";
        system ("$SLEEP 2");
        if ($RETURN == $CHECKBIT)
        {
            print "STEP 5: $TEMPFILE is zipped successfully. \n";
        }
        else
        {
                        print "STEP 5: $TEMPFILE cannot be zipped ! ISCS Maintenance needs to gzip manually ! \n";
        }

        #(6) Rename TEMPFILE2 to BASEFILE (renaming the processed mail file back to original mail file)
        $RETURN = system "$RENAME $TEMPFILE2 $BASEFILE";
        if ($RETURN == $CHECKBIT)
        {
            print "STEP 6: $TEMPFILE2 is renamed to $BASEFILE successfully. \n";
        }
        else
        {
            print "STEP 6: $TEMPFILE2 cannot be renamed to $BASEFILE ! \n";

            #Start sendmail
            $RETURN = system ("$SENDMAIL start");
                    system ("$SLEEP 1");
                    if ($RETURN == $CHECKBIT)
                    {
                            print "sendmail is started successfully. \n";

                #Send email informing maintenance team that task has failed
                            $EMAIL_SUBJECT = '"AUTOMATION TASK: Please inform C755 Maint Team (Hotline: 6214 0674)"';
                            system ("$MAILER -s $EMAIL_SUBJECT $EMAIL_C755 < $EMAIL_FAIL");
                exit;
            }
            else
            {
                print "sendmail cannot be started ! ISCS Maintenance needs to manually start sendmail IMMEDIATELY ! \n";
                exit;   
            }
        }

        #(7) Remove the Duplicate file
        $RETURN = system ("$DELETE $DUPFILE");
        if ($RETURN == $CHECKBIT)
        {
            print "STEP 7: Duplicate mail file is deleted successfully. \n";
        }
        else
        {
            print "STEP 7: Cannot delete duplicate mail file ! ISCS Maintenance needs to delete it manually ! \n";
        }

        #Start sendmail
        $RETURN = system ("$SENDMAIL start");
        system ("$SLEEP 1");
        if ($RETURN == $CHECKBIT)
        {
            print "sendmail is started successfully. \n";

            $date = `/usr/bin/date`;
                    print "NEL_MAILHOUSEKEEPER script has completed at $date \n";
                    print "++++++++++++++++++++++++++++++++++++++++++++++\n";

            #Send email informing the operator that task is completed successfully
            $EMAIL_SUBJECT = '"AUTOMATION TASK: NEL_MailHouseKeeper.pl script run result -> SUCCESSFUL"';
                        system ("$MAILER -s $EMAIL_SUBJECT $BASEFILE < $EMAIL_PASS");
        }
        else
        {
            print "sendmail cannot be started ! ISCS Maintenance needs to manually start sendmail IMMEDIATELY ! \n";
        }   

Hi James 79,
First of, I will advice that you use the following in your code:
use warnings; and use strict;. These two pragma, will save you a lot of headaches.

Secondly, It is a lot better if your codes are divided into different parts using subroutine instead of lumpping all into a single long piece. Such help debugging a lot faster, easier, cleaner and better.

In Perl, you don't top define all the variable you will need. They are define "where" they are needed. Moreover, for all of the variable you defined are actually intrinsic in perl.

Having said all of the above, Please check the following:

#my $CHECKBIT => 0; #you wrote this
my $CHECKBIT = 0;    # I think you mean this?

  ...

    while ( $BUFFER = <FILE1> ) {
        if ( $BUFFER =~ m/^From / ) {
          #my ${FLAG_PRINT} = 1; # You wrote this
          $FLAG_PRINT = 1;       # I think you mean this
            $COUNTER++;
        }

And so $FLAG_PRINT must be defined before the while loop, like this:

    my $FLAG_PRINT = 0;

    while ( $BUFFER = <FILE1> ) {
        if ( $BUFFER =~ m/^From / ) {
            #my ${FLAG_PRINT} = 1;
          $FLAG_PRINT = 1;
            $COUNTER++;
        }

        if ( ( $FLAG_PRINT == 1 ) && ( $COUNTER <= $MAILS_TO_ARCHIVE ) ) {
            print FILE2 $BUFFER;
        }
        elsif (( $FLAG_PRINT == 1 )
            && ( $COUNTER > $MAILS_TO_ARCHIVE )
            && ( $COUNTER <= $TOTAL_MAILS ) )
        {
            print FILE3 $BUFFER;
        }
    }

Also note that the error indicated below has been taken care of in the code just above it...

if (($FLAG_PRINT = 1) && ($COUNTER <= $MAILS_TO_ARCHIVE))
                {
                    print FILE2 $BUFFER;
                }
                elsif (($FLAG_PRINT = 1) && ($COUNTER
                ...

Note the usage: $FLAG_PRINT = 1 instead of $FLAG_PRINT == 1.

Correct these and we might as well be having a "Happy" script afterall.

Hope this helps.

Thank you for your reply and help. I'll try and see if there's any more error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.