Operating System - HP-UX
1753734 Members
4468 Online
108799 Solutions
New Discussion

Re: Route emails in a perl script

 
James R. Ferguson
Acclaimed Contributor

Re: Route emails in a perl script


@allanm77 wrote:
Have another question about the original perl script (from above), I want to use that on three to four different files -
/var/tmp/email & /var/tmp/email_new & /var/tmp/email_newer
with same logic and probably send to different userids with different subjects.
I tried to keep that in the same script but struggled with die statements which used to close the program if it couldn't find the one of the file.

Currently you 'die' (with the reason for the death) if you can't open a file.  That's one way.  I would construct your 'open...or die...' like this, though:

 

...
my $HM_name = '/var/tmp/email';
open NM, .'<', $NM_name or die "Can't open '$NM_name': $!\n";
...

You can also test for the presence of a file and only if present, open it:

 

if ( -e $NM_name ) {
    open NM, '<', $NM_name or die "Can't open '$NM_name': $!\n";
}
else {
    ...
}

 

Notice how we defined the file name as a variable for consistent use throughout, too.  If it's wrong in one place, its the same "wrong" everywhere :-)

 

In the second example code where we test for file existence we are ignoring a race condition where a file does indeed exist and the test is satisfied, but the open fails (and you die) because when that's atttempted another process has removed the file.  I don't believe that you need to worry in a case like this, but you should be aware of the potential if only in the abstract.

 

Regards!

 

...JRF...