### Check and make sure ALL the variables contain values and are NOT == ERROR: if ($ipAddr !~ /ERROR/i && $hostname !~ /ERROR/i && $location !~ /ERROR/i && $timestamp !~ /ERROR/i) { ### Declare the Log File: my $LOG_FILE = "$LOG_DIR/$hostname.log"; ### build the line to print to the LogFile: # *MAX COLUMN WIDTHS --> Timestamp(24) Location(14) IP Address(15) Hostname(18) my $new_data = sprintf "%-25s %-15s %-16s %-19s", "$timestamp", "$location", "$ipAddr", "$hostname"; ### Open the Log File and "TIE" it to the array @logData: # *With this (*Tie::File) whatever happens to the array is reflected in the file: tie my @logData, 'Tie::File', "$LOG_FILE" or die "Error: tie failed, $!"; ### If the File/Array is empty --OR-- THe first element does not contain the LOG_HEADER, then... if ($#logData == -1 || $logData[0] !~ /LAST CHECK|LOCATION|IP ADDRESS|HOSTNAME/gi) { ### Insert the LOG_HEADER as element '0' in the array: unshift @logData, "$LOG_HEADER"; } ### If @logData has more lines then MAXLINES, then Splice out X lines starting at Line 2 (*i.e. index '1') splice @logData, 1, @logData-$MAXLINES if @logData>$MAXLINES; ### Next, push the new data onto the end of the Array/File: push @logData, "$new_data\n"; ### Untie the Array from the Log File (*essentially closing the file...): untie @logData; }