My Perl script to monitor a directory on Unix, stores a list of users to whom a notification mail is sent when the directory it is monitoring is updated.
This is the construct used
dirmon.pl
my $subject = '...';
my $msg = '...';
my $sendto = '[email protected] [email protected] [email protected]';
my $owner = '[email protected]';
...
open my $fh, "|-", "mail", "-s", $subject, $owner, "-c", $sendto
or die "$0: could not start mail: $!";
print $fh $msg or warn "$0: print: $!";
close $fh;
So, right now, for every new user that
I want to send the notification mails to, I need to go to the code and add them to $sendto
. It is fine for me, but I want to distribute the utility to users later and do not want them adding addresses to the list manually, at least not editing the Perl code directly.
There are two alternatives I can think of
Maintaining an external file that has the list of recipients. I can add a flag so that when the user says
dirmon.pl -a [email protected]
, the email address is appended to the file and the next time a mail is sent, the mail goes to this recipient too (dirmon.pl -r [email protected]
to remove the user from the list). The only problem with this is that I need to have one more external file with the script, which I am trying to minimize.I can have self modifying Perl code on the lines of "Can a perl script modify itself?". I am not sure if this is a good idea.
Is first way the best way? Is there any better method to maintain the list of recipients?