fork download
  1. #!/usr/bin/perl
  2. use 5.016;
  3. use warnings;
  4. use Time::Piece;
  5.  
  6. sub slice {
  7. my ($n, $r, @l) = @_;
  8.  
  9. return (@l ? slice($n, [@{$r}, [@l[0 .. $n - 1]]], @l[$n .. $#l]) : @{$r});
  10. }
  11.  
  12. sub calendar {
  13. my ($y, $m, $sub) = @_;
  14.  
  15. return sub {
  16. slice(7, [], (
  17. map{ $sub->($_) }
  18. ((0) x $_[0]->day_of_week),
  19. (1 .. $_[0]->month_last_day),
  20. ((0) x (7 - ($_[0]->day_of_week + $_[0]->month_last_day) % 7))
  21. ))
  22. }->(Time::Piece->strptime("$y-$m-1", "%Y-%m-%d"));
  23. }
  24.  
  25. foreach(calendar(2014, 2, sub{ $_[0] ? sprintf("%2d ", $_[0]) : " " })){
  26. say @{$_};
  27. }
  28.  
Success #stdin #stdout 0.03s 4948KB
stdin
Standard input is empty
stdout
                   1 
 2  3  4  5  6  7  8 
 9 10 11 12 13 14 15 
16 17 18 19 20 21 22 
23 24 25 26 27 28