36 # get a timestamp in the format YYYYMMDDHMS
37 my ($year, $month, $dayofmonth, $hour, $minute, $second)
38 = (localtime())[5, 4, 3, 2, 1, 0];
Why not just assign the variables in the order that localtime returns them:
# get a timestamp in the format YYYYMMDDHMS
my ( $second, $minute, $hour, $dayofmonth, $month, $year )
= localtime;
70 parse_config($path_to_conf_file) if $path_to_conf_file;
100 # declare LOG file handle
101 my $LOG;
102 sysopen ($LOG, $path_to_log_file, O_WRONLY | O_APPEND | O_CREA
+T)
103 or die "Can't open $path_to_log_file: $!";
104 # We'll try and keep a lock on the log file for the duration o
+f our script
105 flock($LOG, LOCK_EX) or die "Can't get a lock on $path_
+to_log_file";
201 if (/^log-file\s*=\s*(.+)$/) {
202 if (-d $1) {
203 warn "in $conf_filename line $.: $1 is a direc
+tory";
204 next;
205 }
206 unless (-e $1) {
207 unless (open(LOG, ">", $1)) {
208 warn "in $conf_filename line $.: Cannot op
+en $1 for writing: $!";
209 next;
210 }
211 print LOG "[" . scalar(localtime()) . "] Creat
+ed logfile\n";
212 }
213 close(LOG);
214 $path_to_log_file = $1;
215 next;
216 }
You call parse_config() on line 70 which opens and prints to and closes the log file and then sets the variable $path_to_log_file which is subsequently used to open the log file again. Do you really need to open the log file inside the parse_config() function?
268 sub logger {
269 my ($message, $level) = @_;
270 return if $level > $log_level;
271
272 # name log levels
273 my @level_names = qw( NONE ERROR WARNING INFO DEBUG );
274
275 # autoflush output for current scope only
276 local $| = 1;
277
278 # DEBUG
279 #print "\@level_names:\n";
280 #print "\t$_\n" foreach @level_names;
281 #print "\$level: $level\n";
282 #print "\$message: $message\n";
283
284 print $LOG "[" . scalar(localtime()) . "] " . $level_names
+[$level] . ": $message\n";
285 }
You are changing the value of the $| variable which turns on autoflush for the currently selected file handle which by default is STDOUT so it will have no effect on the following print statement. Since you are opening the log file with sysopen perhaps you should write the log message using syswrite which bypasses buffered IO.
syswrite $LOG "[" . scalar(localtime()) . "] " . $level_names[$lev
+el] . ": $message\n";
202 if (-d $1) {
203 warn "in $conf_filename line $.: $1 is a direc
+tory";
204 next;
205 }
206 unless (-e $1) {
218 if (-d $1) {
219 warn "in $conf_filename line $.: $1 is a direc
+tory";
220 next;
221 }
222 unless (-e $1) {
To avoid stating the same file twice you can use the special _ filehandle:
202 if (-d $1) {
203 warn "in $conf_filename line $.: $1 is a direc
+tory";
204 next;
205 }
206 unless (-e _) {
218 if (-d $1) {
219 warn "in $conf_filename line $.: $1 is a direc
+tory";
220 next;
221 }
222 unless (-e _) {