Dear Perl Monks,

I just tried to make a simple logger package, having a compilation error with its ver.1 as attached below.

error message

Bareword found where operator expected at /home/tatsu/Perl/MyLog/MyLog +.pm line 36, near "} encode" (Missing operator before encode?) syntax error at /home/tatsu/Perl/MyLog/MyLog.pm line 36, near "} encod +e" Compilation failed in require at logtest.pl line 6. BEGIN failed--compilation aborted at logtest.pl line 6.

MyLog.pm (ver.1)

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;

When a local var $fh was used for the file handle with its print command in the log subroutine (ver.2), however, no error occurred and everything worked as expected. It appears that the error occurs with the 'print $self->{fh}' part, but I am not sure how it comes to the error message 'Bareword found where operator expected...'.

Any advice would be much appreciated.

Sincerely,

MyLog.pm (ver.2, log subroutine only)

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;

logtest.pl

use strict; use warnings; use utf8; use FindBin; use lib $FindBin::Bin; use MyLog; my $mylog = MyLog->new(); $mylog->log(); $mylog->log(''); $mylog->log('a', 'b');

In reply to file handle var for print command by tatsu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.