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

I keep on doing this at the beginning and end of my scripts:
#/usr/bin/perl -w use stict; print "Started: strftime( "%a %b %d %T %Z %Y", localtime )\n"; # stuff done here print "Stopped: strftime( "%a %b %d %T %Z %Y", localtime )\n";
Is there a way I can create a module that wraps this two stuff into my script via a begin and end blocks by only importing it via a 'use' directive?

Replies are listed 'Best First'.
Re: Start / Stop Time Module
by Anonymous Monk on Aug 12, 2010 at 19:19 UTC
    Sure, but first you have to write valid perl
    $ cat EchoStartStop.pm; package EchoStartStop; use POSIX(); BEGIN { print "Started: ",POSIX::strftime( "%a %b %d %T %Z %Y", localtime +),"\n"; } END { print "Stopped: ",POSIX::strftime( "%a %b %d %T %Z %Y", localtime +),"\n"; } 1;
    It might suit your needs to simply
    $ perl -le"print scalar localtime" Thu Aug 12 12:19:10 2010
Re: Start / Stop Time Module
by oko1 (Deacon) on Aug 13, 2010 at 00:09 UTC

    Since you're generating the same output as the 'date' command provides, you could just do it right from the command line:

    $ date; ./myscript; date

    You could also create a macro in whatever editor you're using that would let you stick something like this at the top of your scripts:

    sub x{print localtime()."\n"};BEGIN{x};END{x}

    Not that I'm not actually recommending this as a *good* solution; I suspect you're having an XY Problem, with the concomitant incorrect approach. But since that's not the question you asked...


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re: Start / Stop Time Module
by awohld (Hermit) on Aug 13, 2010 at 01:49 UTC
    This would be good too, it would tell you what the start, stop, and duration of the execution along with the file name. Good if it's a CRON job...

    Would this make a good module? I'd use it, and I offer to do it.
    package Timer::StartStop; use strict; use Time::Elapse; Time::Elapse->lapse( my $now); BEGIN { print "$0 Started: " . ( scalar localtime ) . "\n"; } END { print "$0 Finished: " . ( scalar localtime ) . ", time = $now\n"; } 1;

      That means loading yet another non-core module. Why not use Time::HiRes instead, which is core.

      On a second note, IF you want to make this into a module, consider finding a way to accept options for what to print on start and exit. It might be very interesting to add an option that uses the values returned by times () to show resource use. It's a shame that there is no HiRes version for this function, but you might want to delve into getrusage () in BSD::Resource (which may not work on non-Unix boxes).

      If options like that were implemented correctly, it would certainly be worth releasing to CPAN.

      test.pl started : Fri Aug 13 08:23:10 2010 test.pl ended : Fri Aug 13 08:23:11 2010 duration: 00:00:01.51003 (usr: 0.41, sys: 0.19, cusr: 0, csys: + 0)

      update: being able to select the output (STDOUT or STDERR) would be another point of attention


      Enjoy, Have FUN! H.Merijn