Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

davorg (18)

davorg
  dave@dave.org.uk
http://dave.org.uk/
Yahoo! ID: daveorguk (Add User, Send Message)

Hacker, author, trainer

Technorati Profile [technorati.com]

Journal of davorg (18)

Friday July 12, 2002
05:10 AM

Calendar::Simple

[ #6301 ]

I got an email from someone asking for help building a 2-d array that models the weeks and days in a month. My standard response to questions like this is "check CPAN", but when I checked, I couldn't find something that did just what I wanted. There were plenty that did over-complex things like build HTML tables of months, but nothing that just creates the underlying data structure.

So I wrote one. It's called Calendar::Simple. Here's an example of how you'd use it to write a replacement for the Unix cal utility.

#!/usr/bin/perl -w

use strict;
use Calendar::Simple;

my @months = qw(January February March April May June July August
                September October November December);

my $mon = shift || ((localtime)[4] + 1);
my $yr  = shift || ((localtime)[5] + 1900);

my @month = calendar($mon, $yr);

print "\n$months[$mon - 1] $yr\n\n";
print "Su Mo Tu We Th Fr Sa\n";
foreach (@month) {
  print map { $_ ? sprintf "%2d ", $_ : '   ' } @$_;
  print "\n";
}

It's currently winging its way to CPAN, but in the meantime you can get it from http://dave.org.uk/modules/.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.