Raku By Example
View me onGitHub
# 布尔
:foo .say; # OUTPUT: foo => True
:!foo.say; # OUTPUT: foo => False

# 无符号整数
:2nd   .say; # OUTPUT: nd => 2
:1000th.say; # OUTPUT: th => 1000

# 字符串和语素(看起来像数字的字符串是 Str + numeric 类型)
:foo<bar>      .say; # OUTPUT: foo => bar
:foo<42.5>     .say; # OUTPUT: foo => 42.5
:bar<42.5>.perl.say; # OUTPUT: :bar(RatStr.new(42.5, "42.5"))

# 位置
:foo['foo', 42.5] .say; # 一个可变数组:   OUTPUT: foo => [foo 42.5]
:foo<foo bar 42.5>.say; # 一个不可变列表: OUTPUT: foo => (foo bar 42.5)
# 尖括号让你拥有(allomorphs)变体

# Callables
:foo{ say "Hello, World" }.say;
# OUTPUT: foo => ;; $_? is raw { #`(Block|82978224) ... }


# Hashes; keep 'em simple so it doesn't get parsed as a Callable
:foo{ :42a, :foo<a b c> }.say; # OUTPUT: foo => { a => 42, foo => (a b c)}

# Can use fat-arrow notation too:
# (这里它们周围的圆括号只是用于 say 调用)
(nd => 2).say; # OUTPUT: nd => 2
(foo => ('foo', 'bar') ).say; # OUTPUT: foo => (foo bar)
(foo => %(:42a, :foo<a b c>) ).say;
# OUTPUT: foo => { a => 42, foo => (a b c)}  


my ($a, $b, $c);
:(($a, $b), $c) := ((1, 2), 3);

say "$a, $b, $c";

my ($name, $shares, $price, $year, $mon, $day);

:($name, $shares, $price, ($year, $mon, $day)) := ('ACME', 50, 91.1, (2012, 12, 21) );

say "$year, $mon, $day";


# Pointy loops can also destructure hashes, allowing assignment to variables:
my %hhgttu = (:40life, :41universe, :42everything);
   for %hhgttu -> (:$key, :$value) {
     say "$key$value";
}

# pairs
my $pair = 'jakub' => 'helena';  # "=>" is the pair constructor
my $pair2 = :run('quckily');     # same in adverbial notation
my $pair3 = :eat<food>;          # same using <>, the new qw()
say $pair.key;                    # returns 'jakub'
say $pair2.value ;                # returns 'quckily'
say $pair3.isa(Pair);             # Bool::True