[Purdue-pm] an example Perl 6 program

Mark Senn mark at senn.us
Tue Jan 3 09:30:45 PST 2017


Following my signature is an example Perl 6 program.
I call it month, typing
     month 2017-01
causes
     2017-01-01  Sun
     2017-01-02  Mon
     2017-01-03  Tue
     ...
to be printed.  Typing
     month --help
causes the USAGE message to be printed.

Mark Senn, Systems Programmer, Engineering Computer Network, Purdue 
University


#!/home/pier/e/mark/src/rakudo-star-2016.11/install/bin/perl6

sub MAIN($year-month, Bool :$help)
{
     ($help)  and  USAGE;

     my ($year, $month) = $year-month.split('-');

     (($year ~~ /^^\d\d\d\d$$/)  &&  ($month ~~ /^^\d\d$$/))  or  USAGE;

     my $date = Date.new(year => $year, month => $month, day => 1);
     my $dow = $date.day-of-week;
     my $days = $date.days-in-month;

     # Forty-two days is more than enough.
     my @weekday = flat('', ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 
'Sun') xx 6);

     for (1..$days) -> $day
     {
         printf "%4s-%2s-%02d  %s\n", $year, $month, $day, 
@weekday[$dow++];
     }
}


sub USAGE()
{
     say q:to 'END';
     Usage: month yyyy-mm

     Example: month 2017-01
     END

     exit 1;
}



More information about the Purdue-pm mailing list