#!/usr/bin/perl
use 5.016;
use warnings;
use Time::Piece;
sub slice {
my ($n, $r, @l) = @_;
return (@l ? slice
($n, [@{$r}, [@l[0 .. $n - 1]]], @l[$n .. $#l]) : @{$r}); }
sub calendar {
my ($y, $m, $sub) = @_;
slice(7, [], (
((0) x $_[0]->day_of_week),
(1 .. $_[0]->month_last_day),
((0) x (7 - ($_[0]->day_of_week + $_[0]->month_last_day) % 7))
))
}->(Time::Piece->strptime("$y-$m-1", "%Y-%m-%d"));
}
foreach(calendar
(2014, 2, sub{ $_[0] ? sprintf("%2d ", $_[0]) : " " })){ say @{$_};
}
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwp1c2UgVGltZTo6UGllY2U7CgpzdWIgc2xpY2UgewoJbXkgKCRuLCAkciwgQGwpID0gQF87CgoJcmV0dXJuIChAbCA/IHNsaWNlKCRuLCBbQHskcn0sIFtAbFswIC4uICRuIC0gMV1dXSwgQGxbJG4gLi4gJCNsXSkgOiBAeyRyfSk7Cn0KCnN1YiBjYWxlbmRhciB7CglteSAoJHksICRtLCAkc3ViKSA9IEBfOwoKCXJldHVybiBzdWIgewoJCXNsaWNlKDcsIFtdLCAoCgkJCW1hcHsgJHN1Yi0+KCRfKSB9CgkJCQkoKDApIHggJF9bMF0tPmRheV9vZl93ZWVrKSwKCQkJCSgxIC4uICRfWzBdLT5tb250aF9sYXN0X2RheSksCgkJCQkoKDApIHggKDcgLSAoJF9bMF0tPmRheV9vZl93ZWVrICsgJF9bMF0tPm1vbnRoX2xhc3RfZGF5KSAlIDcpKQoJCSkpCgl9LT4oVGltZTo6UGllY2UtPnN0cnB0aW1lKCIkeS0kbS0xIiwgIiVZLSVtLSVkIikpOwp9Cgpmb3JlYWNoKGNhbGVuZGFyKDIwMTQsIDIsIHN1YnsgJF9bMF0gPyBzcHJpbnRmKCIlMmQgIiwgJF9bMF0pIDogIiAgICIgfSkpewoJc2F5IEB7JF99Owp9Cg==