Hi! Please consider following me on twitter: @hanekomu.

World clock in Perl, functional programming approach

I talk to Perl mongers in different time zones, so I wrote a small program to tell me what time it is in those time zones. Lately I find myself writing programs in a more functional programming-oriented style, that is, avoiding intermediate variables and using grep() and map() a lot. Since Perl is a procedural language at heart, this is possible only to a certain extent. The world clock program is simple enough so I could write it in a purely functional style:

#!/usr/bin/env perl

use warnings;
use strict;
use DateTime;
use Text::Table;

print Text::Table->new(qw/Timezone Time/)->load(
    map { [ $_->time_zone->name, $_->strftime("%F %T %Z") ] }
    map { DateTime->now->set_time_zone($_) }
    qw{
        Europe/London
        Europe/Vienna
        America/Los_Angeles
        Asia/Tokyo
    }
);

The result looks like this:

Europe/London       2008-05-31 10:02:09 BST 
Europe/Vienna       2008-05-31 11:02:09 CEST
America/Los_Angeles 2008-05-31 02:02:09 PDT 
Asia/Tokyo          2008-05-31 18:02:09 JST 

Write a comment | Bookmark and Share

posted at: 10:38 | path: /dev | permalink | 0 comments | 0 trackbacks

Comments are closed for this story.

Trackbacks are closed for this story.