use v6;
say "Hello World";
say "Perl 6 rocks".chars;
my $name = prompt("Please type in yourname: "); say "Hello $name";
my @names = ("Foo", "Bar", "Moo"); say "Hello { join(', ', @names) } how are you?";
Hello Foo, Bar, Moo how are you?
my $age = 23; if $age > 18 { say "You can drink beer with Perl Mongers."; }
if 23 <= $age and $age <= 42 { }
if 23 <= $age <= 42 { }
if $age == 3 or $age == 4 or $age == 5 { }
if $age == 3|4|5 { }
my @fellows = <Foo Bar Baz>; for @fellows -> $name { say $name; }
my @phones = <Foo 123 Bar 456 Moo 789 Foo 976>; for @phones -> $name, $number { say "$name $number"; }
my %phone = ( "Foo" => 123, "Bar" => 456, ); for %phone -> $key, $value { say "$key $value"; }
my @scores = < Valencia 1 1 Recreativo_Huelva Athletic_Bilbao 2 5 Real_Madrid Malaga 2 2 Sevilla_FC Sporting_Gijon 3 2 Deportivo_La_Coruna Valladolid 1 0 Getafe Real_Betis 0 0 Osasuna Racing_Santander 5 0 Numancia Espanyol 3 3 Mallorca Atletico_Madrid 3 2 Villarreal Almeria 0 2 Barcelona >; for @scores -> $home, $home_score, $guest_score, $guest { say "$home $guest $home_score : $guest_score"; }
for (1, 2, 3, 4, 5) -> $x, $y { say "$x $y"; }
1 2 3 4 StopIteration
for (1, 2, 3, 4, 5) -> $x, $y? { say "$x $y"; }
my @chars = <a b c>; my @numbers = <1 2 3>; for @chars Z @numbers -> $letter, $number { say "$letter $number"; } a 1 b 2 c 3
for (1..Inf) -> $i { }
$*PROGRAM_NAME
$*CWD
$*IN
@*ARGS
sub add ($a, $b) { return $a + $b; } say add(2, 3); # returns 5 #say add(2); # is an error #say add(2, 3, 4); # is an error
sub process_template($input, $output, %params) { }
multi sub process_template($input, %params) { } multi sub process_template($input, $output, %params) { }
multi sub process_template($input, $output, %params?) { }
sub abc(Int $x) { say 1 }; abc(2); # 1 abc("a"); # Nominal type check failed for parameter '$x'; expected Int but got Str instead
sub abc($x where {$^a < 2} ) { say $x }; abc(1); # 1 abc(2); # Constraint type check failed for parameter '$x'
say [*] 1..10;
my @x = (1, 2) >>+<< (3, 4); say @x.perl; # [4, 6]
my @x = (1, 2) X+ (4, 7); say @x.perl; # [5, 8, 6, 9]
our sub infix:<rrr>($a, $b) { $a + 2* $b }; say 2 rrr 3; # 8
say [rrr] 1,1,1; # 5
my $c = "What was the colour of the cat?"; if ($c ~~ m/colour/) { say "Matching '$/'"; # 'colour' }
regex num { \d ** 1..3 }; if 10 ~~ m/ <num> / { }
$string ~~ regex { \d+ } $string ~~ token { \d+ } $string ~~ rule { \d+ }
$string ~~ m/^ \d+ $/; $string ~~ m/^ \d+: $/; $string ~~ m/^ <.ws> \d+: <.ws> $/;