http://qs321.pair.com?node_id=279196


in reply to Better use of perl print statment for Nagios

A couple of thoughts

Since it appears that the data you are interested in is always in the same offsets of your split you could define them once, and then use them throught out the rest of the code. (You could also use Constant, but read up on the docs and do a super search, as interesting problems can arise from using the module). You could use the select function to clean up the prints, or a HERE document. If you end up going with HERE docs, I would suggest doing whatever "extra" logic prior to, so that you don't need to end the print, perform a simple test and reinitiate printing.. Ala

# snipped for brevity's sake my $nodename = '0'; my $m_type = '2'; my $comment = '6'; # etc... for ( @MASTER ) { my($chk_cmd, $chk_ping); chomp; my $line = [ split("\t") ]; # If its a t3 array these lines need to be different, do it # now as opposed to later if ( $line->[$m_type] eq 'sun_t3') { $chk_cmd = "check_command\tcheck-host-alive-ping\n"; $chk_ping = "check_command\tcheck_ping\n"; } else { $chk_cmd = "check_command\tcheck-host-alive\n"; $chk_ping = "check_command\tcheck_newping\n"; } print autoconf<<EOF; # Host $line->[$nodename] $line->[$m_type] check host alive define host { use generic-host host_name $line->[$nodename] alias $line->[$comment] address $IPnumber $chk_cmd } EOF }

Also since quite a bit of that content is relativly static, I might end up using an array to store bits and pieces, then joining them into single scalars ala my $out = join("\n", @data); to further clean up the output.

use perl;