Information wants to be free...

Alarm Clock in Perl/Tk

I had a metallic egg-timer that met a gruesome fate, after I discovered that it was in fact made of plastic inside. Instead of buying a new physical timer/alarm clock, I decided to write a virtual one.

Below is a Perl script that will display a countdown, starting from a specified amount of minutes. When the counter reaches zero, the text in the window will blink red, in an attempt to get your attention. To make sure you see this, it is recommended to use the "Always on top" feature of your window manager.

If required, it would probably be easy to add sound support, by just hacking the script to include something like: system("aplay bell.wav");

Note that you need the Perl/Tk module to use the script.

#!/usr/bin/perl -w
use strict;
use Tk;

my $minutes = shift @ARGV
  or die "Usage: $0 <minutes>\n";

my $target = time + ($minutes * 60);

my $window = new Tk::MainWindow;
$window->repeat(50, \&update_display);
my $label = $window->Label(-foreground => "black",
                           -background => "white",
                           -font => 'Helvetica 72 bold')->pack();

Tk::MainLoop;

sub update_display {
  my $left = $target - time; # Seconds left.
  if ($left > 0) {
    $label->configure(-text => sprintf("%02d:%02d:%02d",
      ($left / 3600) % 60, ($left / 60) % 60, $left % 60));
  } else {
    if ($left % 2 == 0) {
      $label->configure(-text => "Alarm!", -foreground => "red");
    } else {
      $label->configure(-text => "Alarm!", -foreground => "black");
    }
  }
}
          


Topic: Scripts and Code, by Kjetil @ 11/07-2008, Article Link