Raku Programming Language
Q: There are new versions of Perl, and I mean Raku (Perl 6). Perl 1, which is being developed here, is retro software. Can you give us some examples of Raku programming?
A: Yes, Perl 1 comes from the year 1987, and Raku is from 2018. That seems like 31 years of development. Here are examples of Raku programming taken from a modern Perl course. Examples have an example usage, the source code, and an example output (or interactive session).
./name.raku
#!/usr/bin/env raku
use v6.d;
sub MAIN() {
my $name = prompt "Wie ist deine Name? ";
say "Willkommen, ", $name, "!"
}
Wie ist deine Name? Matze
Willkommen, Matze!
./nummer_vermutung.raku 50
#!/usr/bin/env raku
use v6.d;
sub MAIN($maximum = 100) {
my $geheimnummer = $maximum.rand.Int;
say "Hinweis: ", $geheimnummer;
loop {
my $vermutung = prompt "Wie ist die Nummer? ";
last unless $vermutung != $geheimnummer;
if ($vermutung < $geheimnummer) {
say "Falsch. Es ist zu wenig.";
} else {
say "Falsch. Es ist zu viel.";
}
}
say "Richtig!";
}
Hinweis: 37
Wie ist die Nummer? 33
Falsch. Es ist zu wenig.
Wie ist die Nummer? 44
Falsch. Es ist zu viel.
Wie ist die Nummer? 37
Richtig!
./umkehren.raku perl
#!/usr/bin/env raku
use v6.d;
# Hier kΓΆnnen Sie Ihre Mitteilung geben.
sub MAIN($mitteilung) {
# Kehr die Mitteilung um.
loop (my $i = $mitteilung.chars - 1; $i >= 0; $i--) {
say substr $mitteilung, $i, 1
}
}
l
r
e
p
./chdir.raku
#!/usr/bin/env raku
use v6.d;
sub MAIN($irgendwo = 'irgendwo') {
# Hier ist genau '$*HOME'.
my $tiefer = $*HOME.add: $irgendwo;
chdir($tiefer) || die $tiefer ~ ': Es gibt nicht was Sie wollen.';
say $*CWD;
}
./join.raku
#!/usr/bin/env raku
use v6.d;
sub MAIN(Str $inputText, Int $times) {
my $outputList = $inputText xx $times.Int;
my $outputText = $outputList.join: ',';
say $outputText;
}
./deck.raku
#!/usr/bin/env raku
use v6.d;
sub MAIN() {
say (< β€ β‘ β’ β§ > X ( 2, 3, 4, 5, 6, 7, 8, 9, 10, 'jack', 'queen', 'king', 'ace' ));
}
./regex.raku βName: Jane Doe, Age: 41, Email: john.doe@email.com ENDβ
#!/usr/bin/env raku
use v6.d;
sub MAIN($input) {
# Name: <John> <Doe>, Age: <25>, Email: <john.doe@email.com>
say $input ~~ rx/"Name: "\w+\s\w+", Age: "\d+", Email: "<-[ \s ]>+/;
}
ο½’Name: Jane Doe, Age: 41, Email: john.doe@email.comο½£
Bio
Q: I read your bio on Twitter. Do the emojis have something in common with Perl?
A: Indeed, those are a result of the following Raku program.
Indeed, those are a result of the following Raku program.
say "β¬" x 73 ~ (for 1..4 -> $i { "β" ~ "π
" x $i }) ~ "πΊ" x 4;
β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β¬ β π β π π β π π π β π π π π πΊ πΊ πΊ πΊ Letβs examine all the parts. First, it prints a black square emoji 73 times. The black or graphite colour should resemble GitHub. The graphite colour encourages writing, and I do mean programming by that. 73 is 80% of 91, and the total number of emoji characters is 91. The widely known 80-20 rule, or the Pareto Principle, was applied. The second part contains a loop (for 1..4 -> $i { βββ ~ βπ β x $i }). It produces a list of 4 atoms in 10 tomatoes. Note that the βtomatoβ word consists only of characters from the βatomβ word. The idea is taken from a modern painting. Atom can mean atheism, and I read a lot about atheism. The last part πΊ is a Japanese goblin. Together with the atom emoji, it can represent atheism. It also means fourfaced, as one can be doublefaced, triplefaced, or even fourfaced. This idea was taken from a discussion about a modern painting. By not sticking to the constants, one can generate more bios. One last thing to do would be to remove the whitespaces. A shortened version of this bio is available on all my GitHub pages. β¬βπ πΊ works usually as a footer but also as a header in the case of my GitHub profile. Letβs consider an alternative bio with a different arrangement ββ¬πΊπ . Atom would stand for atheism and black square for GitHub and programming. The Japanese goblin would represent my version of the Perl 1 compiler and OpenRoguelike. The tomato would represent an interest in compilers that is quite the traditional one. A further compiler worth knowing is Perl 5 and I want to read in German about it.
say "β" x 73 ~ (for 1..4 -> $i { "β¬" ~ "πΊ" x $i }) ~ "π
" x 4;
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β¬πΊ β¬πΊπΊ β¬πΊπΊπΊ β¬πΊπΊπΊπΊ π π π π
βπ π¦ π¦