In Raku, Lists can be constructed several ways.
# A list of strings: "one", "two", "three"
my @x = <one two three>;
# Using << >> allows interpolating variables
my $num = "six";
my @y = << four five $six >>;
Classes get a new
method automatically, where public attributes can be set in a key => "value"
style.
class Person {
has Str $.name;
}
my $p = Person.new(name => "Liz");
The object model allows overriding a TWEAK
, which runs after the new
method, and is convenient
for setting up extra state while still taking advantage of automatic new
.
TWEAK
should almost always be a submethod
(as opposed to a regular method
) so that subclasses don't inherit it.
Examples of overidding TWEAK are in the class bodies below.
By the way: you don't need parens when calling subroutines.
sub foo($x, $y) {
say "x: $x and y: $y";
}
foo(123, 456);
foo "blah", "blargh";
There's always more than one way to do it! Embrace and master the chaos.
You can pass blocks of code to subroutines, and they're indicated with the &
sigil.
sub wrap-call(&b) {
say "wrappin!";
&b();
}
wrap-call({ say "nice" })
# prints:
# wrappin!
# nice
You can match lots of stuff with the ~~
smart match operator. A string against a regex literal
for instance.
my $x = "Rolling Stones";
$x ~~ /Stone/;
if ?$/ { # equivalent to $/.Bool
say "I matched against the string";
}
The $/
is the "Match Variable", which is set to the result of the previous match. The ?
prefix
operator coerces it to a boolean.