Category: Utility Scripts
Author/Contact Info Adam Monsen <adamm@wazamatta.com>
http://adammonsen.com
Description: Simple console-based stopwatch. Assumes 80-character wide terminal. Stop with Control-C. Requires Time::Duration.
#!/usr/bin/perl -w
use strict;
use Time::Duration qw( duration );

# $Id: stopwatch,v 1.2 2003/03/28 07:44:01 adamm Exp $

$| = 1;
$SIG{'INT'} = sub { exit(0) };

sub running_time() {
  my $total = time - $^T;
  return duration($total);
}

print "Started at: ", scalar localtime($^T), "\n";

while (1) {
  my $string = scalar(localtime)." [".running_time()." elapsed.]";
  my $spaces = " " x (80 - length($string));
  print $string, $spaces;
  sleep 1;
  print "\r";
}

END {
  print "\nRan for ", running_time(), "\n";
}