Perl 为什么连在一起的电子邮件地址不是';打印不正确吗?
我有一个Perl脚本,它从文件中加载名称和电子邮件地址,然后尝试将它们连接到一个字符串中以用于发送电子邮件。电子邮件地址行必须是以逗号分隔的地址列表,格式为Perl 为什么连在一起的电子邮件地址不是';打印不正确吗?,perl,Perl,我有一个Perl脚本,它从文件中加载名称和电子邮件地址,然后尝试将它们连接到一个字符串中以用于发送电子邮件。电子邮件地址行必须是以逗号分隔的地址列表,格式为Name,… 我希望它能最终产出 To: John and Julie <john@example.com>, John and Julie <julie@example.com>, Bobby and Liz <bobby@example.net>, Kevin and Jayme <kevin32
Name,…
我希望它能最终产出
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>,
下面是我的Mac上运行最新版本的Sierra和Perl 5.18.2的程序的完整输出
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
根据您的输出,我怀疑电子邮件地址后面有一个“\r”字符。检查email_config.txt中的行结尾,或删除\r字符
sub init {
...
(my $email_addresses = $+{email_addresses})=~s/\r//g;
@{$self->{emails}} = split(';', $email_addresses);
...
}
欢迎来到SO。请以您看到的形式(非内联)发布您的预期输出以及您得到的结果。如果有任何编辑可以让你更接近a,我们都会更好。当我运行你的代码时,看起来我得到了你想要的。也许您正在运行您没有粘贴到问题中的代码?当我运行您的代码时,我得到了您期望的输出。所以问题似乎出现在您的环境中。@Jeff6次我粘贴的代码正是我正在运行的代码,我得到的输出粘贴在下面。我在最新版本的Mac OS Sierra上运行perl 5.18.2。您应该通过编辑问题来添加信息,特别是当问题是多行数据时。但这不是问题的根源吗?这不是问题的根源,但除非您确切了解Perl子例程的功能,否则不应该在Perl子例程上使用原型;它们不同于其他语言中的原型。因此,
sub-main{…}
etc.是正确的。问题似乎在于\r。我更改了EmailConfig.pm以使用不同的正则表达式,这样它就不会一直读到最后,在这之后,它就可以正常工作了。我的$emailConfigRegex=qr/(?*)=(?(\b[A-Za-z0-9.\uz%+-]+\@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b+,?)/;
#!/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
sub init {
...
(my $email_addresses = $+{email_addresses})=~s/\r//g;
@{$self->{emails}} = split(';', $email_addresses);
...
}