Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 使用data::dumper在终端上打印电子邮件_Perl_Email_Data Dumper - Fatal编程技术网

Perl 使用data::dumper在终端上打印电子邮件

Perl 使用data::dumper在终端上打印电子邮件,perl,email,data-dumper,Perl,Email,Data Dumper,即使在阅读了Perl文档并查看了git中的其他脚本之后,我也不理解如何使用Data::Dumper。我在网上看到了很多处理哈希的例子,但我认为这与我需要做的不太相符 我正在创建一个脚本,向经理或团队发送有关被解雇员工的电子邮件。我被告知在我的代码中添加print Dumper$email,这样当使用--dry_run选项时,我们可以在终端上看到电子邮件的打印输出--dry_run还将确保电子邮件不会实际发送。当我运行perl--dry_run时,什么都没有发生。也许我需要按照$d=Data::D

即使在阅读了Perl文档并查看了git中的其他脚本之后,我也不理解如何使用Data::Dumper。我在网上看到了很多处理哈希的例子,但我认为这与我需要做的不太相符

我正在创建一个脚本,向经理或团队发送有关被解雇员工的电子邮件。我被告知在我的代码中添加
print Dumper$email
,这样当使用--dry_run选项时,我们可以在终端上看到电子邮件的打印输出--dry_run还将确保电子邮件不会实际发送。当我运行
perl--dry_run
时,什么都没有发生。也许我需要按照
$d=Data::Dumper->new(
)的思路做点什么

以下是我的代码片段:

#!/usr/bin/perl
use strict;
use warnings;
use NIE::Email;
use Data::Dumper;
use List::Util qw(any);
use Getopt::Long;
Getopt::Long::Configure qw(gnu_getopt);
my ($qa, $verbose, $dry_run, $help, $dbh);

GetOptions(
    'qa' => \$qa,
    'verbose|v' => \$verbose,
    'dry_run' => \$dry_run,
    'help|h' => \$help
);

#Generate email here
sub mail_func {
    print "Prepare email\n" if $verbose;
    my $n = shift;    #user
    my $i = shift;    #ips
    my $t = shift;    #testnets
    my $m = shift;    #managers (multiple if owner is undef)
    my @to_list;      # send to field
    foreach my $value (values %{$t}) {
        if ($value ne 'lab@abc.com') {    #don't send this email to lab@
            if (any { $value eq $_ } @to_list) { #check not already listed
                next;
            }
            else { push(@to_list, $value); }
        }
    }
    foreach my $key (keys %{$m}) {
        if ($key ne 'def') {
            if (any { $key eq $_ } @to_list) {
                next;
            }
            else { push(@to_list, $key . '@abc.com'); }
        }
    }
    my @body;
    while (my ($key, $value) = each %{$i}) {
        my $b = "IP " . $key . " : Testnet " . $value . "\n";
        push(@body, $b);
    }
    my $sub1 = "Ownership needed!";
    my $sub2 = "Ownership needed script special case";
    my $email;

    #Email testnet group (if not lab) as well as manager of term employee
    if (@to_list) {
        $email = NIE::Email->new(
            From       => 'do-not-reply@abc.com',
            To         => join(',', @to_list),
            'Reply-to' => 'def@abc.com',
            Subject    => $sub1,
            );
        $email->data(
            "Good Day, \n\n The below machines need claimed as their previous"
              . " owner, $n, is showing up as no longer with the company. \n"
              . "Please visit website to change"
              . " ownership of these machhines. \n\n"
              . "@body \n\n"
              . "If you have already requested an ownership change for these"
              . " machines, please disregard this message."
              . "\n\n Thank you \n -Lab team \n\n"
              . "This script is under active development and could contain"
              . " bugs, so please speak up if you have doubts or something "
              . "looks strange."
              . "\n Script name: lab_ownership_needed_email");
        if ($dry_run) {print Dumper($email);}
        else {$email->send();}
    }

如果您能帮助我理解如何使用此脚本,我将不胜感激。谢谢。

恢复为原始版本,重新添加代码,重新运行脚本,它就能正常工作。 上述代码是正确的。
首先感谢simbabque,他说代码看起来是正确的。

你的代码看起来很好。你真的调用了下面的sub
mail\u func
吗?看起来你忘了告诉你的程序实际做些什么。它只会编译,但不会执行,因为subs中的原因只有在你调用它之后才会运行sub.添加
打印转储程序(\@到\u列表)怎么样
大约20行?什么叫
mail\u func
?@simbabque子mail\u func在其他地方被调用。如果你认为有帮助的话,我可以为你打印我的整个脚本。单独运行脚本就像电子邮件发送一样有效,只是--dry\u run选项不起作用。然后我们不需要看到其余的。脚本在哪里t退出脚本go?如何调用它?请显示完整的命令行。因此,如果STDOUT转到电子邮件,则您的
打印转储程序也将转到那里。请尝试
警告转储程序
。这将写入STDERR并显示在命令行上。