public
Created

Small and attractive Perl 6 snippets

  • Download Gist
snippet1.p6
Perl6
1 2 3 4 5 6 7 8 9 10 11 12
my $birth-date-string = prompt "When were you born (yyyy-mm-dd)?";
my $birth-date = Date.new($birth-date-string);
 
my $today = Date.today;
my $age = $today.year - $birth-date.year;
 
# But your birthday this year may not have been yet
if $today.day-of-year < $birth-date.day-of-year {
$age--;
}
 
say "You are $age years old!";
snippet2.p6
Perl6
1 2 3 4 5 6 7
my @scale = ("$_$_" for 0..9, <a b c d e f>);
my @zeroes = '00' xx 16;
 
# Fade from red to blue
for @scale.reverse Z @zeroes Z @scale -> $r, $g, $b {
say "#$r$g$b";
}
snippet3.p6
Perl6
1 2 3 4
for 10 ... 0 -> $count {
say "$count...";
LAST { say "Liftoff!" };
}
snippet4.p6
Perl6
1 2 3 4 5
my $sentence = "I am going to go feed the the cat.";
 
if $sentence ~~ / (\w+) \h+ $0 / {
say "Duplicate word '$0 $0' found at position $/.from().";
}
snippet5.p6
Perl6
1 2 3 4 5 6 7 8
my @beers = 'beers', 'beer', 'beers' xx 97;
 
for reverse(1..99) Z reverse(0..98) -> $n, $nn {
say "$n @beers[$n] on the wall!";
say "$n @beers[$n]!";
say "Take one down, pass it around,";
say "$nn @beers[$nn] on the wall!";
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.