use File::Temp; use constant STATUS_DIR => '/tmp/status-dir'; { my $fh; # hidden from outer code # status is logged only when you have created # the status dir by hand; this lets you turn off # logging by deleting the status dir if ( -d STATUS_DIR ) { $fh = new File::Temp( template => "$$-XXXXXX", dir => STATUS_DIR, suffix => ".txt", unlink => 1 ) or die "can't open temp file: $!"; $fh->autoflush(1); } sub log_status { print $fh "$$ - ", scalar localtime, " - ", @_, "\n" if $fh; } sub close_status { $fh->close if $fh; } }