St, Int, Rat, List, Array, Hash)my @dice = (1..6).roll(2); if @dice.join eq '12' | '21' { # ^^^^^^^^^^^ Junction say 'Mäxchen!'; }
any oder |: mindestens ein Trefferall oder &: alle müssen passenone oder ^: genau ein Treffernone: kein Treffer
sub greet($who) { say "hello $who" } greet("GPW"); # hello GPW say &greet.WHAT; # Sub()
sub up-join($sep, *@items) { join $sep, map &uc, @items } say up-join ", ", <a b c> # A, B, C
sub square(Numeric $x) { $x * $x } say square(42); # 1764 my $x = 'foo'; square($x); # BOOM
Int: beliebig große IntegerNum: 64 bit floating pointsRat: BruchzahlenReal: Int, Num, RatComplex: Komplexe ZahlenNumeric: Complex, Real
Str: (Unicode-)StringsBuf: Binäre stringsList, ArrayEnumMap, Hash
sub a(Int $x) { }; a "foo" # ===SORRY!=== # CHECK FAILED: # Calling 'a' will never work with argument # types (str) (line 1) # Expected: :(Int $x)
subset Positive of Real where { $_ > 0 }; sub circle-area(Positive $r) { $r * $r * pi; } say circle-area(3); # works say circle-area(-3); # BOOM # Constraint type check failed for parameter '$r' # in sub circle-area at /home/moritz/subset.pl:2
multi to-json(Real $x) { $x.Str } multi to-json(@x) { '[' ~ @x.map(&to-json).join(', ') ~ ']' } ...
where ...
my @even := (0..*).map: { $_ * 2 }; say @even[10]; # 20 my @es:= @even.grep: { $_.sqrt.Int ** 2 == $_ }; say @es[10]; # 400
my @powers := 1, 2, 4 ... *; constant fib := 1, 1, { $^a + $^b } ... *; say @powers[10]; # 1024 say fib[10]; # 89
{ $^a + $^b } ist unangenehm zu tippen* + ** wird durch einen Parameter ersetztconstant fib = 1, 1, *+* ... *;
@numbers.map(*.sqrt)
class Task { has &.action; has Bool $.done = False; has @.dependencies; method do { last if $.done; .do for @.dependencies; # resolve dependencies &.action(); # perform the action $!done = True; } }
my $root = Task.new( :action({ say 'Eat breakfast' }), :dependencies[ Task.new(:action({ say 'Prepare breakfast' })), Task.new(:action({ say 'Set the table' })), ] ); $root.do; # output: Prepare breakfast Set the table Eat breakfast
class Task { ... # method 'add-dependency' is handled # by @!dependenies.push has @.dependencies handles add-dependency => 'push'; ... } ... $root.add-dependency( Task.new( :action( { say 'Wake girlfriend' } ) ); );
role Real { # for real numbers method abs { self <= 0 ?? -self !! self } method sqrt { self.Num.sqrt } ... } class Int does Real { } say 10.sqrt; # 3.16227766016838
role A { method x { } }; role B { method x { } }; class C does A does B { }; # BOOM: # Method 'x' must be resolved by class C because it exists in # multiple roles (B, A)
role A { method x { ...} # tatsächlich ... hier }; class B does A { }; # BOOM: # Method 'x' must be implemented by 'B' because it is # required by role A
my $text = 'This contains a double double word'; if $text ~~ / « (\w+) \s+ $0 » / { say "Duplicate word '$0' found"; } # output: Duplicate word 'double' found
grammar Number { token TOP { ^ [ <int> | <hex> ] $ } token int { \d+ } token hex { '0x' (<[a..f A..F 0..9]>+) } } Number.parse('0xDEADBEEF'); say $/.keys; # hex say $<hex>[0].Str; # DEADBEEF
|-Alternativen gewinnt der längste Match
ENDE