perltutorial
boo_radley
<h4>Problem</h4>
You want to display data online in a monthly view.
<h4>Solution</h4>
Make use of [cpan://HTML::CalendarMonthSimple] to provide a programmatic interface to the contents of an HTML calendar month.
<code>#! c:/perl/bin/perl.exe
use strict;
use CGI qw (header);
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use HTML::CalendarMonthSimple;
my $myyear=2001;
my $mymonth=10; # month to pull data on
#
# set up database connectivity & retrieve records
# the dbi method selectall_arrayref was added in DBI v1.15.
#
my $dbi=DBI->connect("DBI:mysql:host=myhost:user=nobody") || die ("no dbi",DBI->errstr);
$dbi->do ("use mydatabase") || die DBI->errstr;
my @items = @{$dbi->selectall_arrayref (
"SELECT title, dayofmonth(date), detail
FROM table
WHERE month (date) = $mymonth AND
year (date) = $myyear
order by dayofmonth(date)")};
#
# set up the calendar & populate information
#
my $cal = new HTML::CalendarMonthSimple('year'=>$myyear,'month'=>$mymonth);
$cal->width('100%');
$cal->border(2);
$cal->header('Monthly data');
$cal->bgcolor('whitesmoke');
#
# populate calendar with data
#
foreach my $row (@items) {
$cal->addcontent($$row[1], "<b>$$row[0]</b> <br /> <i>$$row[2]</i>");
}
print header;
print $cal->as_HTML;
</code>
<h4>Discussion</h4>
This short example provides the barest methods for inputting data into an HTML::CalendarMonthSimple object, and the displaying that calendar.<br/>
The new method takes 2 optional arguments, year and month. If either is missing, the current month or year will be used. This can be useful if you want a calendar of the current month in a previous year.<br />
<b>Example : </b><code> my $cal = new HTML::CalendarMonthSimple ('year'=>1990);</code> will make a calendar for October 1990.<p>
content is added to each day via the setcontent and addcontent methods; setcontent replaces any existing data, and addcontent appends to the day's data.
<b>Example : </b><code>$cal->setcontent(31,"Halloween!"); </code> puts "Halloween" into the 31st day of the calendar.<br>
<b>Example : </b><code>$cal->addcontent(31,"<br>Buy lots of candy for the kids"); </code> appends a second line to the same date.<br>
In addition to specifying the exact day, references like "2FRIDAY" can be used to place information in certain cells when the date value may not be known (think paydays, for example).<p>
In addition to the methods listed above, there are a large host of methods which control the layout & presentation of the calendar; colors, fonts and html attributes can be configured easily, and calendar layout can be manipulated as well.<br>
There are some things which could be done better -- this module is not warning-safe due to concatenation of undef'd strings in a few places, and the HTML produced is not in strict compliance with HTML 3 standards, causing tidy to produce warnings. I feel that these are not insurmountable problems though, and have found that working with HTML::CalendarMonthSimple has been a rewarding and speedy way to present data over the web.<p>
<b>update</b> I've patched the source code to prevent the warnings from occuring and as a side effect, this will emit smaller html. I've contacted the module's owner about the patches.<p>
<b>update (20Nov01)</b> The patches I submitted have been integrated into the module, so it should be warning safe now.