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

print out email on terminal using data::dumper

即使阅读了 Perl 文档并查看了 git 中的其他脚本,我仍然不明白如何使用 Data::Dumper。我在网上看到很多处理哈希的例子,但我认为这不太适合我需要做的事情。

我正在创建一个脚本,用于向经理或团队发送有关离职员工的电子邮件。我被告知将 print Dumper $email 添加到我的代码中,以便在使用 --dry_run 选项时,我们可以在终端上看到电子邮件的打印输出。 --dry_run 还可以确保电子邮件并未实际发送。当我 运行 perl <script> --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,他首先声明代码看起来是正确的。