我有一个Perl脚本,它从文件中加载名称和电子邮件地址,然后尝试将它们连接成一个用于发送电子邮件的字符串。电子邮件地址行必须是逗号分隔的地址列表,格式为Name <email address>, ...
。
我希望它能产生
的最终输出To: John and Julie <john@example.com>, John and Julie <julie@example.com>, Bobby and Liz <bobby@example.net>, Kevin and Jayme <kevin3248@example.com>, Kevin and Jayme <jayme8396@example.com>, Ellen and Mike <mike397987@example.com>, Ellen and Mike <ellen397286@example.com>,
但它只生产
>,
以下是运行最新版Sierra和Perl 5.18.2的Mac程序的完整输出。
on master* perl example.pl
EmailConfig initialized: John and Julie = john@example.com,julie@example.com
EmailConfig initialized: Bobby and Liz = bobby@example.net
EmailConfig initialized: Kevin and Jayme = kevin3248@example.com,jayme8396@example.com
EmailConfig initialized: Ellen and Mike = mike397987@example.com,ellen397286@example.com
john@example.com
julie@example.com
bobby@example.net
kevin3248@example.com
jayme8396@example.com
mike397987@example.com
ellen397286@example.com
>, on master* <ellen397286@example.com
我似乎正在正确阅读所有电子邮件,因为我可以单独打印每个电子邮件,但我不确定脚本中的问题是连接还是打印最终的连接值。
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use EmailConfig;
use File::Slurp;
# Turn off output buffering
$| = 1;
my @emailConfig = ();
sub main() {
loadConfig();
processDataAndSendEmail();
}
main();
sub loadConfig() {
my @raw_configs = read_file('email_config.txt');
foreach my $raw_config ( @raw_configs ) {
my $newEmailConfig = new EmailConfig();
$newEmailConfig->init($raw_config);
push @emailConfig, $newEmailConfig;
}
}
sub processDataAndSendEmail() {
my $to = '';
foreach my $config ( @emailConfig ) {
foreach my $email ( @{ $config->{emails} } ) {
print "$email\n";
$to .= "$config->{name} <$email>, ";
}
}
print "To: $to";
}
package EmailConfig;
use strict;
use warnings FATAL => 'all';
my $emailConfigRegex = qr/(?<email_name>.*) = (?<email_addresses>.*)/;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
sub init {
my $self = shift;
my $emailConfigValue = shift;
if ($emailConfigValue =~ $emailConfigRegex) {
$self->{name} = $+{email_name};
@{$self->{emails}} = split(';', $+{email_addresses});
}
my $print_emails = join(",", @{$self->{emails}});
print "EmailConfig initialized: $self->{name} = $print_emails\n";
}
1;
John and Julie = john@example.com;julie@example.com
Bobby and Liz = bobby@example.net
Kevin and Jayme = kevin3248@example.com;jayme8396@example.com
Ellen and Mike = mike397987@example.com;ellen397286@example.com
答案 0 :(得分:0)
从您的输出中,我怀疑有一个&#39; \ r&#39;电子邮件地址后的字符。检查email_config.txt中的行结尾,或删除\ r \ n字符。
sub init {
...
(my $email_addresses = $+{email_addresses})=~s/\r//g;
@{$self->{emails}} = split(';', $email_addresses);
...
}