There are many things that you could do to make this more readable/maintainabe.

You could avoid having to use the filehandle on all your print statements by using perlfunc:select to make your file.

You can avoid having to explicitly put "\n" on the end of each line by setting local $\="\n" (see perlvar:$OUTPUT_RECORD_SEPERATOR). This can also be achieved using -l (el) on the command line or shebang line.

You could avoid having to use the print keyword on every line by using multiline quotes. Eg.

print "this is line 1 $somevar this is line 2 $someothervar this is line 4 this is line 5 ";

You could avoid the long block initialising all the individual variables using array slices.

use constant { NODE=>0, TYPE=>1, COMMENT=>2 ...}; my @d; @d = ( split '\t' )[ 0,2,6,8,10,12,16,18,20,22,24,26 ]; select AUTOCONF; print " # Host @d[NODE, TYPE, CHECK, HOST, ALIVE] define host{\n use generic-host host_name $d[NODE] alias $d[COMMENT] address $d[IP] max_check_attempts 10 notification_interval 7 notification_period 24x7 notification_options d,u,r } ";

Here docs or formats might be even better, but the whole thing is screaming out for a template system like Text::Template.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


In reply to Re: Better use of perl print statment for Nagios, (Long) by BrowserUk
in thread Better use of perl print statment for Nagios by emjga1

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.