Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on


By "arguments to a subroutine", I'm referring to passing a special value that the subroutine could match against to determine what it should do. For example, below is a quick attempt to pull out your e-mail handling and move it to a separate function, along with how I might call it.

Here's where I would be making use of the function, which I called send_mail() just to stick with what I'd used previously:
open( my $fh, "<", $array_ip_list ) or send_mail("ip_list_error"), die "IP List File - nas_array_ip_list.txt not found"; # [ . . . Do lots of stuff here . . . ] # If our external commands completed successfully, send our e-mail if ($result_from_nas_check_function) { send_mail( "results", $filename ); # [ Clean up after ourselves, delete our temp file, etc. ] exit 0; } else { # [ Notify e-mail that an error occured ] # [ Clean up after ourselves, delete our temp file, etc. ] die "Something went wrong."; }

And here's an example of how I might define the send_mail() function, where I tell it what kind of e-mail it is sending, and then let it do the work:

# Expects at least 1 argument matching the type of e-mail to send, and # optionally the additional arguments it might need. If this got much # more complicated, it might be worth passing the second argument as a # hash to simplify multiple values coming in. sub send_mail { # These could go at the beginning of your script, I just put them # here to make this a more complete example. use MIME::Lite; MIME::Lite->send( "smtp", "" ); my $type = shift; # what type of message should we send? my @args = @_; my $from = ''; my $to = ''; my $msg; if ( $type eq "ip_list_error" ) { $msg = MIME::Lite->new( From => $from, To => $to, Data => "Error:nas_array_ip_list.txt:$!.$ +^E\n", Subject => "IP List File - nas_array_ip_list +.txt - " . "Not Found For $0 Script on $ENV{COMPUTE +RNAME}\n", ); } elsif ( $type eq "results" ) { my $filename = $args[0]; $msg = MIME::Lite->new( From => $from, To => $to, Subject => "Automated Check For NAS DataMover Health.", Type => 'Multipart/mixed', ); $msg->attach( Type => 'TEXT', Path => $filename, Filename => $filename, Disposition => 'inline', ); } else { die "send_mail - bad type: $type\n" } # This will send our e-mail, regardless of which one we built. $msg->send; }

Update: Added the final else, thanks to wfsp's excellent suggestion.

In reply to Re^2: Seeking guidance for more idiomatic way of (re)writing this script. by topher

and:  <code> code here </code>

  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (9)
    As of 2014-12-28 04:47 GMT
    Find Nodes?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?

      Results (178 votes), past polls