Akoya has asked for the wisdom of the Perl Monks concerning the following question:

I encountered something interesting today that has me puzzled. I received this warning message:

Bareword found where operator expected at dcwatcherd line 465, near "$LOG _timestamp" (Missing operator before _timestamp?)

referring to this line of code:

print $LOG _timestamp(), q{ }, join ':', $0, $msg_level, $msg;

The first thing I tried worked--I modified the line of code as follows:

print $LOG q{}, _timestamp(), q{ }, join ':', $0, $msg_level, $msg;

My question is: "Why would Perl warn 'Bareword found where operator expected' when it encountered the function call immediately following the indirect filehandle?"

A few things to note:
  1. There was no warning, and everything worked fine, when I used a bareword filehandle e.g., print LOG _timestamp(), ...
  2. Everything works fine with the indirect filehandle as long as I put something before the function call.
  3. Everything works fine on a newer (5.8.8) version of Perl, on my development platform, without having to make this change.

Comments/questions appreciated --Akoya

Replies are listed 'Best First'.
Re: Bareword warning question
by runrig (Abbot) on Jul 12, 2007 at 22:46 UTC
    I get this warning in version 5.6.1. Hey, it's a bug. Oh, well. It's hard to parse perl. Upgrade or deal with it. That's the way it goes sometimes :-)
    use strict; use warnings; my $fh = \*STDOUT; print $fh foo(1), "\n"; sub foo { shift; }
      Thanks for the quick reply. I guess I'll just have to deal with it. As I mentioned, the workaround of putting anything before the function call works fine. Unfortunately, upgrading is not an easy path on a production server. Akoya
Re: Bareword warning question
by bloonix (Monk) on Jul 13, 2007 at 01:18 UTC
    Hello Akoya,

    I have just a little suggestion for this code part:
    print $LOG _timestamp(), q{ }, join ':', $0, $msg_level, $msg;
    Maybe you like to use something like
    use Log::Handler; my $log = Log::Handler->new( filename => $logfile, mode => 'append', timestamp => $your_format, prefix => "$0:<--LEVEL-->:" ); $log->warning($msg); # or another msg_level
    Kind regards, bloonix
Re: Bareword warning question
by ysth (Canon) on Jul 13, 2007 at 01:20 UTC
    Does the warning go away if you say &_timestamp() instead? How about if you have a forward declaration ( sub _timestamp; ) at the beginning of the file?
Re: Bareword warning question
by ikegami (Patriarch) on Jul 12, 2007 at 22:38 UTC

    Everything works fine on a newer (5.8.8) version of Perl

    I think much older versions of Perl required barewords for file handles. Could you be using such a version? I can't replicate your problem with 5.6.0+
      It could be something with my build of Perl 5.6.0, which is running on Solaris 2.6. Unfortunately, upgrading is not an easy path on a production server. The simple workaround that I mentioned does work for me, though. Thanks for the quick reply.
Re: Bareword warning question
by Anonymous Monk on Jul 13, 2007 at 06:01 UTC