Professional Resume Template for Software Developers
Perl6 for-beginners
1. Perl6 for Beginners
Jens Rehsack
Niederrhein Perl Mongers
European Perl Conference 2019
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 1 / 32
2. Introduction Motivation
Motivation
Collecting (Lost) People
Perl 6 is in development for more than 10 years and it has a lot of impressive
new features.
The language syntax evolved with the language capabilities and the evolved
mindset.
Getting snippets of code from experienced Perl 6 developers might create
more (new) questions than answers (if any).
This talk attempts to start at the beginning and collects everyone on the way.
Goals
At the end of the talk, attendees should be able to write small Perl 6 code and
understand given examples.
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 3 / 32
3. Introduction Is & Isn’t
What Perl 6 Is
Perl 6 Is . . .
a modern language of the Perl family
a language with a fast growing module ecosystem
a development environment with built-in support for asynchronouos I/O, auto
parallelization, multiple syntaxes in one program, . . .
a name of a open minded, strong collaborating community
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 4 / 32
4. Introduction Is & Isn’t
What Perl (6) Is not
Perl 6 Isn’t . . .
Perl 4
Perl 5
intended to be a drop-in replacement of any of them
continuity of ”Status Quo”
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 5 / 32
5. Introduction Is & Isn’t
What Rakudo Is
Rakudo Is . . .
a Perl 6 compiler (targeting Moar VM and JVM)
Perl 6 Specification conform
one of many Perl 6 implementations
Rakudo* Is . . .
a bundle of the Perl 6 compiler and curated Perl 6 ”core” modules
what you might call ”perl”
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 6 / 32
6. Starting
Getting & Installation (Binaries)
macOS & Windows
Go to https://rakudo.org/files
Choose your Operating System or Windows
Follow the installation instructions for your Operating System or Windows
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 7 / 32
7. Example Code Running Perl 6
Starting Perl 6 Program
How to run Perl 6 Programs
Run explicitely with compiler:
sno@ernie$ perl6 prog.p6
Run self-contained
sno@ernie$ head -n1 ./ prog.p6
#!/opt/pkg/bin/perl6
sno@ernie$ chmod +x ./ prog.p6
sno@ernie$ ./ prog.p6
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 8 / 32
8. Example Code Running Perl 6
Starting Program With Parameters
How to run Perl 6 Programs With Parameters
Run explicitely with compiler:
sno@ernie$ perl6 prog.p6 --arg1 --arg2=val pos_arg
Run self-contained
sno@ernie$ head -n1 ./ prog.p6
#!/opt/pkg/bin/perl6
sno@ernie$ chmod +x ./ prog.p6
sno@ernie$ ./ prog.p6 --arg1 --arg2=val pos_arg
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 9 / 32
9. Example Code Running Perl 6
Starting Perl6 with Parameters
Starting Perl6 with Parameters
Perl 6 specifies in https://github.com/perl6/specs/blob/master/S19-
commandline.pod which parameters are available and what they do.
A more detailed OptionReference is available later on that page - so scroll
down and read until the end!
However, Rakudo implements some options (-M) and refuses some options
(-F) - so always refer to perl6 --help
Change perspective: Some things are better solved differently than adding
options perl6-debug ./prog.p6 replaces perl -d ./prog.pl
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 10 / 32
10. Example Code One Liners
Killer App: One Liners
Doing One Liners with Perl 6
Perl has always been famous for it’s one-liners. Let’s continue . . .
Strip trailing whitespaces perl6 -pe ’s/s*$//’ Looks familiar, doesn’t it?
Most use is very likely the line ending tidying - done easily in Perl 6 by
perl6 -pe ’’ Yes - it’s completely built-in. An empty by-line job does
perl5 -p -i -e ’s/012?015/n/g’
Another famous job is filtering, like perl6 -ne ’.say if .is-prime’
Adding line numbers to some inputfile needed? How about
perl6 -ne ’for lines.kv -> $no , $l {
say sprintf ("%04d: %s", $no , $l) }’
Maybe a bit more flexibility in width of line-numbers?
perl6 -e ’my @l = lines;
my $fmt = "%0" ~ @l.elems.chars ~ "d: %s";
for ^@l.elems {say sprintf($fmt , $_+1, @l[$_]) }’
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 11 / 32
11. Example Code Strings
Defining Strings
Strings – Synopsis
dd ’I can see $$ in your eyes ’;
dd "I am N° $*PID";
my $s = "Haa"; $s++; say " Interpolated <$s>!";
dd q:to/TERMINATOR/
Lorem ipsum dolor sit amet , consectetur adipisici elit ,
sed eiusmod tempor incidunt ut labore et dolore magna
aliqua.
TERMINATOR
This encompasses a literal string – the string will be processed exactly as
written.
That string encompassing causes interpolation. Mind the $*PID instead of
the $$ from Perl 5 or Bourne Shell constructs.
Increments Haa → Hab and prints Interpolated <Hab>!
Heredocs are included into q and qq, respectively
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 12 / 32
12. Example Code Strings
C special characters & substr
Strings – Synopsis
my $s = "a, b, e, f, n, r, t, , x1b , cJ";
my $bell = substr($s , 0, 1); dd $bell;
my $nl = substr($s , *-1, 1); dd $nl;
my $tab = substr($s , 18..18); dd $tab;
my $lf = substr($s , 15, * -12); dd $lf;
. . . well known list of special characters for terminal controlling.
Getting the first character (bell)
Getting the last character (newline)(how many newlines are in that string?)
Fetching a range of 1 character containing a tabulator special char from the
middle
Beware negative addressing of to — getting line feed character
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 13 / 32
13. Example Code Strings
”Attributes” of Strings
Strings – Synopsis
my $s = "c[WAVING WHITE FLAG , VARIATION SELECTOR -16,
ZERO WIDTH JOINER , RAINBOW]";
say $s;
say "chars: ", $s.chars;
say "codes: ", $s.codes;
say "encode.bytes: ", $s.encode.bytes;
say "encode(’utf -16 ’). bytes: ", $s.encode(’utf -16 ’). bytes;
. . . on a typical unicode character ( ).
It is 1 character
containing 4 codes
using 14 bytes in default (UTF-8) encoding
or 12 bytes in UTF-16 encoding.
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 14 / 32
14. Example Code Strings
String quoting
Strings – Synopsis
my $l = Q (SELECT COUNT (*) FROM users );
my @f = Q:x:ww/ls/;
my $e = q<swept volume > turbocharger >
my $i = qq:!h[%0{@f.elems.chars}d: %s]
Literal string – with nested delimiters.
Literal string → executed → split on words (with quote protection)
Almost literal string, but allows escaping (e.g. to ”fix” delimiters used in
string)
Fully interpolated string – except hashes for this special case.
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 15 / 32
15. Example Code Strings
Working with strings
Strings – Synopsis
my $s = chr (45) x 80; say $s;
say "c[WAVING WHITE FLAG , VARIATION SELECTOR -16,
ZERO WIDTH JOINER , RAINBOW]".ords;
my ($s1 , $s2);
$s1 = ’Hello ’; $s2 = "Worldn";
print $s1 ~ $s2;
$s = "heLlo wOrld";
say "uc: ", $s.uc , " -- tc: ", $s.tc , " -- lc: ", $s.lc;
Outputs a textart line of 80 ’-’
Tells all ordinal values of the four codepoints of $s (127987, 65039, 8205,
127752)
Concatenates the most important strings when learning a new language
UPPER cases, Title cases or lower cases "heLlo wOrld"
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 16 / 32
16. Example Code Strings
Match and Replacing
Strings – Synopsis
my $r = "expletive expression";
$r ~~ s/expletive/regular /;
my $s = "Hello world";
my $g = ~$/ given $s ~~ m/^(w+)/;
(my $gpc = $s) ~~ tr/oleH/terG /;
$gpc.substr -rw(6,5) = "PerlCon";
Text processing!!! Not swearwords . . .
What’s our greeting word?
Hello → Greet
Greet PerlCon
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 17 / 32
17. Example Code Numbers
Int/Num Literals
Numbers – Synopsis
say 10_00000 == 1_000_000;
say 1000 000 - 1e6;
my $db = 0xdeadbeef;
chmod 0o644, $?FILE;
say 0o755 - :3 <200021 >;
say -42;
Four ways to note your first million - mind the underscore don’t carry any
semantic information
Integer in hexadecimal notation
. . . and as typical in such a situation an octal value
arbitary bases can be used, too
unary operator applied to literal . . . but nitpicking
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 18 / 32
18. Example Code Numbers
Complex Literals
Numbers – Synopsis
say i;
say 17i;
say 47+11i;
say -4+Infi;
say 13-8i;
say 2* -3-2i;
say 2* <-3-2i>;
0+1i - square root of -1
0+17i - only the imaginary part
47+11i - half real and half imaginary Genuine Eau de Cologne
-4+Infi - negative complex number with infinity imaginary part
13-8i - positive complex number with negative imaginary part
-6-2i - not a literal but mind operator precedence
-6-4i - or better write complex number as literal
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 19 / 32
19. Example Code Numbers
Rational Literals
Numbers – Synopsis
my $p = 3.1415 9265; $p = pi; $p = π;
my $t = τ;
my $e = e;
say 1
⁄2.perl , ", ", <1/7>.perl;
Calculate 2 * π * r or π * r2 using the correct constant instead of
hard-coded NIH value
τ=2π
Euler’s number . . .
Outputs 0.5, <1/7>
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 20 / 32
20. Example Code Numbers
Basic operations
Numbers – Synopsis
say $t <=> $e; # <, <=, ==, >=, >, !=, <=>
say π+e; # +, -, *, /, %, **, %%
say $p++, " -- ", ++$p; # ++, --, -, +, ^, ?, ~
say 1 +< 4; # +<, +>, +&, +|
say π2 ; # 1 2 3 4 5 6 7 8 9 0
say 21 2 7 -1; # all directly appended superscripted belong tog
comparison operators
binary arithmetic operators
prefix – postfix operators . . . and other unary operators
shift left as shortcut for power of 2 - binary bitwise operators
direct power of arbitary base
can be combined to a very large prime number
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 21 / 32
21. Example Code Conditionals
Conditionals - If
If – Synopsis
if True { say "Yes" } else { say "No" };
if incisors and swimming {
if broad_tail { say "Beaver (Castor)" }
elsif flattened_tail { say "Muskrat" }
else small_tail { say "Coypu" }
}
say "c[LIFE WITHOUT ALFA ROMEO]" un less Alfa -Driver;
say "c[LIFE WITH ALFA ROMEO]" if Alfa -Driver;
A very ordinary if . . . else line
A larger if-block containing some if, elsif (not, but . . . ) and finally an
else
statement modifier printing for unfortunate ones (currently missing in
unicode)
statement modifier printing for fortunate ones (currently missing in
unicode, too)
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 22 / 32
22. Example Code Conditionals
Conditionals - With
With – Synopsis
with "abcdefg".index (("a".."g"). roll) { .say }
without (Any , True ). roll { say "Roll again" }
sub prime -only { my $v = (^2019). pick;
$v.is -prime || return Any; return $v }
with prime -only () -> $n { say "$n is prime" }
my $s = ("a".."g"). roll;
with $s.index("a") { say "say <b> now" }
orwith $s.index("b") { say "did you say <a>?" }
else { say "neither <a> nor <b> was said" }
says a number from 0 ...6
50% chance to get Roll again
way lower chance to see . . . is prime using pointy block syntax together with
with
analogous to if ...elsif ... you can concatenate multiple tests using
orwith after with
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 23 / 32
23. Example Code Conditionals
Conditionals - Given/When
Given – Synopsis
given (2, 3, 7, "Any", Any). pick {
when 2 { say $_ * 3 * 7; }
when 3 { say $_ * 2 * 7; }
when 7 { say $_ * 2 * 3; }
when "Any" { say "Any" }
default { say "Hmpf"; }
}
60% chance to get the answer to the ultimate question of life, the universe,
and everything
20% chance to get Any answer
20% chance to disappoint the automate
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 24 / 32
24. Example Code Conditionals
Conditionals - Given/When
Given – Synopsis
given (2, 3, 7, "Any", Any). pick {
when Int { say "Int found , computing ..."; }
when 2 { say $_ * 3 * 7; }
when 3 { say $_ * 2 * 7; }
when 7 { say $_ * 2 * 3; }
when Str { say "Str found , checking ..."; }
when "Any" { say "Any" }
default { say "Hmpf"; }
}
The block surrounding when or default statements will be left immediately
after leaving the sub-block after when or default statement.
Read: 1st when block skips 2nd, 3rd and 4th when block,5th when block
skips 6th when block
when statement modifiers won’t cause leaving a block
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 25 / 32
25. Example Code Conditionals
Conditionals - Proceed/Succeed
Given – Synopsis
given (2, 3, 7, "Any", Any). pick {
when Int { print "Int found , computing ..."; proc eed; }
when 2 { say $_ * 3 * 7; }
when 3 { say $_ * 2 * 7; }
when 7 { say $_ * 2 * 3; }
when Str { say "Str found , checking ..."; proc eed; }
when "Any" { say "Any" }
default { su cceed; say "Hmpf"; }
}
Now there is a 60% chance to get the answer to the ultimate question of life,
the universe, and everything computed
20% to get now a checked Any - Str as output
In Any other case (default), the automate immediately succeeds and keeps
its disappointment for itself
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 26 / 32
26. Example Code Conditionals
Loops
for – Synopsis
my @l = (^10). pick (3);
for @l { .say }
for @l <-> $n { ++$n }
for @l -> $a , $b = "n/a" { say "a: $a , b: $b"; }
@l = (^10). pick (4). sort;
for @l { say "$^y > $^x" }
loop over elemenst in @l, put the current one into $ and execute the block
running $ .say
run a similar loop and write results back
just another pointy block with default value . . . – outputs something like
2, 7
8, n/a
loops over @l taking two elements at once and put them into ($x, $y) –
alphabetically ordered
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 27 / 32
27. Example Code Conditionals
Loops
loop – Synopsis
loop (my $i = 0; $i < 10; ++$i) { $i.say }
loop { say ’forever ’ }
my @l = loop (my $i = 10; $i < 20; ++$i) { $i + $i - 1 }
say @l;
(loop (my $i = 10; $i < 20; ++$i) { $i * 2 - 1 }). say;
loop as known from C programming language
easy to write infinite loops - no need for hacks
loops can be used to produce results . . .
. . . which can be processed immediately
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 28 / 32
28. Example Code Conditionals
Loops
while – Synopsis
my $i = 1; my $p = 1;
while $i < 5 { ++$i if (++$p).is -prime }
say "$p is the {$i}th prime";
my $x = 1;
"{$x **2} <= {2** $x}".say and ++$x whi le $x**2 <= 2**$x;
while runs the loop as long as the expression evaluates to true
expression is evaluated before the block is executed – might result in entire
block is skipped
while can be used as statement modifier, too
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 29 / 32
29. Example Code Conditionals
Loops
until – Synopsis
my $i = 1; my $p = 1;
until $i > 5 { ++$i if (++$p).is -prime }
say "$p is the {$i}th prime";
my $x = 1;
$x++ until "$x".chars > 2;
until runs the loop as long as the expression evaluates to false
expression is evaluated before the block is executed – might result in entire
block is skipped
until can be used as statement modifier, too
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 30 / 32
30. Example Code Conditionals
Loops
repeat . . . while/until – Synopsis
my $i = 0;
repeat { $i++ } while $i < 10;
say $i;
repeat while $i < 10 { $i++ };
say $i;
repeat { $i++ } until "$i".chars > 2;
say $i;
repeat runs the block at least once – expression is evaluated after the block
has been run
repeat ...while runs as long as the expression evaluates to true
repeat ...until runs as long as the expression evaluates to false
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 31 / 32
31. Thank you
Thank You For Listening
Questions?
Jens Rehsack <rehsack@cpan.org>
Cologne
Jens Rehsack (Niederrhein.PM) Perl6 for Beginners European Perl Conference 2019 32 / 32