-1

I am not understanding how to use Data::Dumper even after reading the Perl doc and looking at other scripts in git. I see lots of examples online dealing with hashes, but I didn't think that quite fit with what I need to do.

I am creating a script to send emails to managers or teams regarding terminated employees. I was told to add print Dumper $email to my code so that when --dry_run option is used, we could see on the terminal a printout of what the email would look like. --dry_run would also ensure that the email isn't actually sent. When I run perl <script> --dry_run, nothing happens. Maybe I need to do something along the lines of $d = Data::Dumper->new(?

Here is a snippet of my code:

#!/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();}
    }

Any help in understanding how to use this for my purpose would be greatly appreciated. Thank you.

Jes
  • 75
  • 7
  • 2
    Your code looks fine. Are you actually calling that sub `mail_func` further down? Looks like you forgot to tell your program to actually do something. It will just compile, but then not execute things, because cause in subs will only be run once you call that sub. – simbabque Apr 11 '18 at 15:47
  • 1
    What about adding `print Dumper(\@to_list)` about 20 lines up? What's calling `mail_func`? – Matt Jacob Apr 11 '18 at 15:50
  • @simbabque the sub mail_func is being called elsewhere. I can print my whole script for you if you think that will help. running the script by itself works as the emails do send, it is just the --dry_run option that doesn't work – Jes Apr 11 '18 at 16:22
  • 2
    Ok. Then we don't need to see the rest. Where does the STDOUT of the script go? How do you invoke it? Please show your complete command line. – simbabque Apr 11 '18 at 16:31
  • @simbabque I am really new to Perl and this is my first script attempt. I am not sure how to see where STDOUT goes...I assume it is the Email library. I call it simply `perl ownership_needed.pl` or `perl ownership_needed.pl --dry_run` – Jes Apr 11 '18 at 16:51
  • 1
    So if STDOUT goes to the email, then your `print Dumper` will go there, too. Try `warn Dumper` instead. That'll write to STDERR and should show up on the command line. – simbabque Apr 11 '18 at 16:54
  • That did not work either. – Jes Apr 11 '18 at 16:59
  • I'm voting to close this question as off-topic, because you haven't provided code to reproduce the problem in the question. – Matt Jacob Apr 11 '18 at 19:01
  • That's fine to close it. I offered to post my entire code and not just the email section, and it was declared fine not to. As simbabque said the code looked fine, I just decided to redo it one step at a time this morning. Going back to my original and by the end, --dry_run option works. I am not sure why it didn't yesterday and now does, but my finished code is the same as above. So I thank simbabque for stating it looked right, which pointed me in the right direction. – Jes Apr 12 '18 at 15:28

1 Answers1

1

Reverted to original, re-added in code, re-ran the script, and it works. The above code is correct as is. Thanks to simbabque who stated the code looked correct in the first place.

Jes
  • 75
  • 7