use utf8; use Time::Piece; use Carp 'croak'; use Encode 'encode'; sub new { my $class = shift; my $self = {}; bless $self, $class; my $timesig = localtime->strftime("%y%m%d%H%M%S"); my $logfile = $timesig . '.log'; if (-f $logfile) { croak "log file ${logfile} already exists.: $!"; } open my $fh, '>>', $logfile or croak "can't open log file ${logfile}.: $!"; $self->{fh} = $fh; return $self; } sub DESTROY { my $self = shift; close $self->{fh}; } sub log { my $self = shift; my $timesig = localtime->strftime("%y%m%d%H%M%S"); # my $fh = $self->{fh}; foreach my $msg (@_) { print $self->{fh} encode('utf8', $timesig . ': ' . $msg . "\n"); # print $fh encode('utf8', $timesig . ': ' . $msg . "\n"); } } 1;