Perl Calendar > HTML

This perl script generates the HTML for a simple 12-month calendar. Run it in Terminal: it will ask which day of the week is the first of January. Then, copy-and-paste the results to your HTML code. Modify the script as you like.


#usr/bin/perl -w

# generate a calendar for HTML
# sharris@umass[dot]edu 2017

# ___________________________________________ declarations

my $start = 0;
my @monthNames = qw(January February March April May June July August September October November December );
my @month = qw(31 28 31 30 31 30 31 31 30 31 30 31);
my @dayNames = qw(Su M Tu W Th F Sa);

my $table = “<table width=’200′ border=’1′ cellpadding=’2′ cellspacing=’1′ bordercolor=’#000000′>”;
my $tr = “<tr align=’center’ valign=’middle’>”;

my $weekday = 0; # which day of the week is it?
my $date = 1; # what date is it?
my $m = 0; # total months for giant loop offset by 1
my $offset = 0; # offset counter
my $i = 7; # 7 days in a week

# ____________________________________________ setup
print “\nOn which day of the week is the first of January? (M T W H F S U)”;
$start = <STDIN>;

# now we set the week-counter to the day of the week minus 1 as offset

if ($start == “M”){$offset = 1;}
if ($start == “T”){$offset = 2;}
if ($start == “W”){$offset = 3;}
if ($start == “H”){$offset = 4;}
if ($start == “F”){$offset = 5;}
if ($start == “S”){$offset = 6;}
if ($start == “U”){$offset = 0;}

# ___________________________________________ BIG LOOP

foreach (@monthNames) {
print “<p><b>”.$_.”</b></p>\n”;
print $table;
print $tr;

foreach (@dayNames){
print “\n<td bgcolor=’#CCCCCC’><b>”.$_.”</b></td>”;
}
print “</tr>\n”;

$date = $month[$m] – ($month[$m] + $offset) + 1;

until ($date > $month[$m]){
for ($i = 7; $i > 0; $i–){
if ($i == 7){print “<tr align=’center’ valign=’middle’>”;}
print “<td bgcolor=’#CCCCCC’>”;
if ($date > 0 && $date <= $month[$m]){print $date;} else {print “\&nbsp\;”;}
print “</td>\n”;
if ($i == 1){print “</tr>”;}

if ($date == $month[$m]){$offset = 8 – $i;}
$date++;
} # weekly loop

} # fill loop
print “</table>”;
$m++;
} # monthly loop

Leave a Reply