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

I am sending the output from my script to a text file, and would like that file to become the log of each action my script has taken. I tried to use this for obtaining the time: my $now = (localtime(time)); Then used the $now variable in my printf commands to re-produce the time like this: printf " $now - Creating interactive session... "; The problem is that $now is only returning whatever the static time WAS when the my $now = (localtime(time)); command executed. I need the $now variable to return a NEW timestamp that is accurate for the second the variable is sent to the output file. Please help! Thanks.

Replies are listed 'Best First'.
Re: How can I timestamp each printed statement?
by Roy Johnson (Monsignor) on Mar 06, 2008 at 19:09 UTC
    There are a whole bunch of logging modules on CPAN. Log::Dynamic looks suitable to my undiscerning eye.

    Update: You can get the effect you want — having $now implicitly call localtime — by tie-ing it:

    package Now; require Tie::Scalar; our @ISA = qw(Tie::StdScalar); sub FETCH {scalar localtime} package main; tie my $now, 'Now'; print "The time is $now\n"; sleep 3; print "The time is $now\n";
    It's probably not what you really want to do, though.

    Caution: Contents may have been coded under pressure.
      THANK YOU VERY MUCH!!! That is doing just what I needed it to. Now each line item of my output that contains $now is returning an accurate timestamp for that action. Thanks again for the quick and useful response!
Re: How can I timestamp each printed statement?
by merlyn (Sage) on Mar 06, 2008 at 20:28 UTC
    Things are really flexible with Log::Log4Perl. You can timestamp things in various ways, set various notification levels and delivery methods (file, email, syslog, etc). If you're logging, it's really the right choice.
Re: How can I timestamp each printed statement?
by igelkott (Priest) on Mar 06, 2008 at 19:15 UTC
    print scalar localtime, " - Creating interactive session... \n";

    The arguments to localtime are optional and scalar makes it return a string instead of an array.

Re: How can I timestamp each printed statement?
by NetWallah (Canon) on Mar 06, 2008 at 19:20 UTC
    Initialize..
    my $now = sub (return scalar(localtime time)); # Creates a subroutin +e reference
    Use as:
    print $now->(), .... # Makes a call to the sub, each time $now->() +is used

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: How can I timestamp each printed statement?
by andreas1234567 (Vicar) on Mar 06, 2008 at 20:58 UTC
    Log::Log4perl is an excellent logging framework that would probably fit well for you too:
    $ cat 672543.pl use warnings; use strict; use Log::Log4perl; my $conf = q( log4perl.category.my_logger = INFO, ScreenAppender log4perl.appender.ScreenAppender = Log::Log4perl::Appender:: +Screen log4perl.appender.ScreenAppender.stderr = 1 log4perl.appender.ScreenAppender.layout = PatternLayout log4perl.appender.ScreenAppender.layout.ConversionPattern=[%p] %d %m +%n ); Log::Log4perl::init( \$conf ); my $log = Log::Log4perl::->get_logger(q(my_logger)); $log->debug(q{one}); $log->info(q{two}); $log->warn(q{three}); $log->error(q{four}); $log->fatal(q{five}); __END__ $ perl -w 672543.pl [INFO] 2008/03/06 21:56:07 two [WARN] 2008/03/06 21:56:07 three [ERROR] 2008/03/06 21:56:07 four [FATAL] 2008/03/06 21:56:07 five $
    --
    Andreas