Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
  • You've got 2 places where you send e-mail. If you do something twice, it's a good candidate for splitting out into a function. I'd probably write a function called "send_mail()" or something, and put all of your e-mail work there. You can call it with an option to tell it what type of e-mail it should send.

  • As someone else mentioned, if most of your code is in an else {} block, that's a red flag that you might want to refactor. Since the initial if {} block is basically just checking to make sure you opened the file, I'd probably do something like:

    open(my $fh , "<" , $array_ip_list) or send_mail("ip_list_error"), die "IP List File - nas_array_ip_li +st.txt not found";

    Where send_mail() is the function mentioned earlier. The send_mail() function will check its argument, see "ip_list_error" and know what to do. This way, you either successfully open the file, or you send your e-mail and exit.

  • Definitely pull out any "global" settings and put them together at the front. That'll make it significantly easier to update later. As mentioned, a hash can be useful for this. You can use a single hash if you don't have too many things, or a couple of them if it makes logical sense.

  • Since you're deleting dmcheck.txt at the end and only using it as a temporary holding place for your e-mail, you'd be better off to use File::Temp. It's a standard module since 5.6.1, and gives you a safer way of using a temp file.

  • Minor style point: in my experience, most Perl programmers don't use parentheses around the conditional in a postfix if statement. For example:

    next if /foo/;

    If you're doing anything much more complicated than that, you'll want to use parens as appropriate of course.

  • Other than that, you're off to a good start already, and there's a bunch of other good suggestions here, too.


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (10)
    As of 2014-12-21 17:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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





      Results (106 votes), past polls