http://www.perlmonks.org?node_id=1055240


in reply to RFC: beginner level script improvement

sub sendmail { my ($REPORT,$CONFIG,$RESULT) = @_; my $T = gettime(); my $MAIL_TO = $CONFIG{MAIL_TO}[0]; delete $CONFIG{MAIL_TO}[0]; my @MAIL_CC = $CONFIG{MAIL_TO};

You pass in three arguments ($REPORT, $CONFIG and $RESULT) but you never actually use these references inside this subroutine.

delete $CONFIG{MAIL_TO}[0] changes the current value of $CONFIG{MAIL_TO}[0] to undef and returns the previous value, it does not change the size of the array.    What you probably really want to do is:

my $MAIL_TO = shift @{ $CONFIG{ MAIL_TO } };

You assign the scalar value $CONFIG{MAIL_TO} to an array but you should just assign it to a scalar variable:

my $MAIL_CC = $CONFIG{ MAIL_TO };


if ($RESULT == 1){ ... }else{ $CONFIG{MAIL_DATA} .= "ERROR: TACACS SERVICE VERIFICATION FAIL +ED!\nReason:"; if ($RESULT == 1){ $CONFIG{MAIL_DATA} .= "HUBSITE: $CONFIG{HUB +SITE} is unreachable\n";} if ($RESULT == 2){ $CONFIG{MAIL_DATA} .= "Provided Username/pa +ssword combination failed Auth +entication\n";} if ($RESULT == 2){ $CONFIG{MAIL_DATA} .= "Provided Username/pa +ssword combination passed Auth +entication, but tacacs profile doesn't allow privileged mode\n";}

The else block of if ($RESULT == 1){ means that $RESULT is NOT equal to 1 but you still test for that.

Why do you have two tests for $RESULT == 2?

Replies are listed 'Best First'.
Re^2: RFC: beginner level script improvement
by georgecarlin (Acolyte) on Sep 23, 2013 at 14:29 UTC
    Thank you very much for pointing out these errors. I also didn't realize that my $var outside of Blocks was a file-wide declaration, I assumed its scope was limited to "Non-Blocks". Now it makes so much more sense =) Thank you for pointing that out! I'm still in the process of rewriting the improved version so this helps me a lot.