-1

即使在阅读 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();}
    }

对于理解如何将其用于我的目的的任何帮助将不胜感激。谢谢你。

4

1 回答 1

1

恢复到原始,重新添加代码,重新运行脚本,它可以工作。上面的代码是正确的。感谢 simbabque,他一开始就说代码看起来是正确的。

于 2018-04-12T15:33:14.583 回答