Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I have this line in multiple files (Perl Code) and every time someone moves or the email changes from anybody I have to go crazy looking for each individual's email address to update the code. I would like to turn this into a external little application where I could control these names without having to go nuts trying to find where they are in all my code.
Could someone give me any idea(s) on how I could apply that if the values for the emails would be coming from an external program?

# Sendmail requires 'To' addresses to be in an array my @mail_to = ($pro_box) ? ( 'person1@mycompany.com', 'person2@mycompany.com', 'person3@mycompany.com', 'person4@mycompany.com') : ( 'person +2@mycompany.com', 'person +3@mycompany.com', 'person +4@mycompany.com');


Thank you for the Help!!!

Replies are listed 'Best First'.
Re: External Array of Emails
by Fletch (Bishop) on Sep 21, 2007 at 13:10 UTC

    One simple way would be to remove the list from your purview completely and have the sysadmin maintain the list in an alias in your mail server.

    However if you're determined to keep it under your control (not that that's a bad thing, just saying . . . :) you could use a YAML file (see also here) with a mapping of sequences (read hash of arrays) for the two cases.

Re: External Array of Emails
by Sinistral (Monsignor) on Sep 21, 2007 at 13:19 UTC

    One of the simplest things to do is to simply use a configuration file that contains all the e-mails that you wish to use, read that, build an array from it, and use that in your @mail_to array.

    use strict; use warnings; use IO::File; my @email_to; my $config_file_name = "emails.txt"; my $config_file = new IO::File; $config_file->open("< $config_file_name") or die("Could not open confi +guration file $config_file_name\n"); while (<$config_file>) { chomp; push @mail_to, $_; }

    It looks like you have a conditional for $pro_box, which has a differing list. In that case, simply use a separate file for the pro list and you're all done

    There are modules to handle configuration files (Config::General springs forth after q quick search), but for this purpose is like using a bazooka to kill a fly.

      Thanks, but see that's where the problem is, the logic, how to make the results of the array here:
      push @mail_to, $_;
      be like this line:
      my @mail_to = ($pro_box) ? ( 'person1@mycompany.com', 'person2@mycompany.com', 'person3@mycompany.com', 'person4@mycompany.com') : ( 'person +2@mycompany.com', 'person +3@mycompany.com', 'person +4@mycompany.com');

        Then use the same variable to control which e-mail configuration file you're using

        my $config_file_name; if ($pro_box) { $config_file_name = 'pro_box_email_list.txt'; } else { $config_file_name = 'not_pro_box_email_list.txt'; }

        The rest of the code I supplied follows after this

Re: External Array of Emails
by n8g (Sexton) on Sep 21, 2007 at 13:07 UTC

    My first instinct is to tell you to centralize these emails either in a text file or database and have your various programs go grab the values as needed. This would give you a central point where you can make updates.

    Having said that you may have good reason not to take the above approach (although one escapes me right now). In that case I would write a script that takes 2 args (oldEmailAddress newEmailAddress) and loops through all of my source code files using a regex to update them. I'm not sure how an external program would know to do this unless you were able to modify it as well but good luck.

Re: External Array of Emails
by dwm042 (Priest) on Sep 21, 2007 at 20:54 UTC
    I think Perl Best Practices recommends the module AppConfig.pm (Available through CPAN and also PPM if you're using Active State). I was curious if a solution could be reached that way; this is an implementation of AppConfig for this kind of problem.

    The config file is named email.conf and contains:

    # # My email configuration file # pro_box = 1 [pro_box] emails = person1@mycompany.com emails = person2@mycompany.com emails = person3@mycompany.com emails = person4@mycompany.com [default] emails = person2@mycompany.com emails = person3@mycompany.com emails = person4@mycompany.com
    And the code to handle it is:

    #!/usr/bin/perl use warnings; use strict; use AppConfig qw( :argcount ); my $config = AppConfig->new( {CREATE => 1, GLOBAL => { ARGCOUNT => ARGCOUNT_LIST }, }); my $config_file = "email.conf"; $config->define("pro_box", { ARGCOUNT => ARGCOUNT_ONE } ); $config->file($config_file); # # Fetch pro box value # my $pro_box = $config->pro_box; # # Fetch arrays. Get rid of the "1" in @array[0]. # my @pro_emails = @{$config->pro_box_emails}; shift @pro_emails; my @default_emails = @{$config->default_emails}; shift @default_emails; print "Pro Box = $pro_box \n"; print "\n" ; my @mail_to = ($pro_box) ? @pro_emails : @default_emails ; for (@mail_to ) { print "My addresses are: $_ \n"; }
    The output from the code is:

    C:\Code>perl test_app_config.pl Pro Box = 1 My addresses are: person1@mycompany.com My addresses are: person2@mycompany.com My addresses are: person3@mycompany.com My addresses are: person4@mycompany.com