Raku By Example
View me onGitHub
# Raku supports writing a MAIN subroutine that is invoked at startup.
# Automatically maps arguments to parameters and generates usage instructions.


multi sub MAIN($number, Bool :$upto) {
    my @fib = 1, 1, *+* ... Inf;
    if $upto {
    say join ',', @fib[0 ..^ $number];
    }
    else {
        say @fib[$number - 1];
    }
}

multi sub MAIN(Str $name) { " " }

#`(
> perl6 MAIN.p6 10
55

> perl6 MAIN.p6 10 --upto
Usage:
  MAIN.p6 [--upto] <number>

# 可选参数写在必选参数前面                                                              
> perl6 MAIN.p6 --upto 10
1,1,2,3,5,8,13,21,34,55
)

## named auguments in MAIN

multi sub MAIN(:$source, :$target, :$count, :$debug) {
    say "source: $source" if $source.defined;
    say "target: $target" if $target.defined;
    say "count:  $count"  if $count.defined;
    say "debug:  $debug"  if $debug.defined;
}

## positional arguments in MAIN

multi sub MAIN($source, $target, $count, $debug) {
    say "source: $source";
    say "target: $target";
    say "count:  $count";
    say "debug:  $debug";
}


## calendar with MAIN

    constant @months = <January February March April May June July
                        August September October November December>;
    constant @days = <Su Mo Tu We Th Fr Sa>;


    sub center(Str $text, Int $width) {
        my $prefix = ' ' x ($width - $text.chars) div 2;
        my $suffix = ' ' x $width - $text.chars - $prefix.chars;
        return $prefix ~ $text ~ $suffix;
    }

    multi sub MAIN(:$year = Date.today.year, :$month = Date.today.month) {
        my $dt = Date.new(:year($year), :month($month), :day(1) );
        my $ss = $dt.day-of-week % 7;
        my @slots = ''.fmt("%2s") xx $ss;

        my $days-in-month = $dt.days-in-month;
        for $ss ..^ $ss + $days-in-month {
            @slots[$_] = $dt.day.fmt("%2d");
            $dt++
        }

        my $weekdays = @days.fmt("%2s").join: " ";
        say center(@months[$month-1] ~ " " ~ $year, $weekdays.chars);
        say $weekdays;
        for @slots.kv -> $k, $v {
            print "$v ";
            print "\n" if ($k+1) %% 7 or $v == $days-in-month;
        }
    }

#`(	
     # April 2014
# Su Mo Tu We Th Fr Sa
       # 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 29 30
)

## most common unique

sub MAIN($file1, $file2) {
    my $words1 = bag slurp($file1).comb(/\w+/).map(*.lc);
    my $words2 = set slurp($file2).comb(/\w+/).map(*.lc);
    my $unique = ($words1 (-) $words2);
    for $unique.list.sort({ -$words1{$_} })[^10] -> $word {
        say "$word: { $words1{$word} }";
    }
}

## ps command

sub MAIN( $string ){                   # 1
    my $p = run 'ps', 'auxww', :out;   # 2
    my $header = $p.out.get;           # 3
    say $header, '-' x 80;             # 4

    for $p.out.lines {                 # 5
        next unless m/ $string /;      # 6
        .trim;                         # 7
        my @v = .words;                # 8
        next if @v[1] == $*PID;        # 9
        .say;                          # 10
    }
    say '-' x 80, $header;             # 11
}