in reply to Better use of perl print statment for Nagios

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