SlideShare a Scribd company logo
1 of 31
Download to read offline
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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

More Related Content

What's hot

Insecure coding in C (and C++)
Insecure coding in C (and C++)Insecure coding in C (and C++)
Insecure coding in C (and C++)Olve Maudal
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)Olve Maudal
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionProf. Wim Van Criekinge
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesGuido Wachsmuth
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by ExampleOlve Maudal
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionEelco Visser
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl coursepartiernlow
 
Quize on scripting shell
Quize on scripting shellQuize on scripting shell
Quize on scripting shelllebse123
 
What every beginning developer should know
What every beginning developer should knowWhat every beginning developer should know
What every beginning developer should knowAndy Lester
 
CLI, the other SAPI
CLI, the other SAPICLI, the other SAPI
CLI, the other SAPICombell NV
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolutionEelco Visser
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsEelco Visser
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshopneosphere
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingEelco Visser
 

What's hot (20)

Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
Insecure coding in C (and C++)
Insecure coding in C (and C++)Insecure coding in C (and C++)
Insecure coding in C (and C++)
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
runtimestack
runtimestackruntimestack
runtimestack
 
Unix
UnixUnix
Unix
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl courseparti
 
Quize on scripting shell
Quize on scripting shellQuize on scripting shell
Quize on scripting shell
 
What every beginning developer should know
What every beginning developer should knowWhat every beginning developer should know
What every beginning developer should know
 
CLI, the other SAPI
CLI, the other SAPICLI, the other SAPI
CLI, the other SAPI
 
Php operators
Php operatorsPhp operators
Php operators
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolution
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
 
C# slid
C# slidC# slid
C# slid
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | Parsing
 
Perl slid
Perl slidPerl slid
Perl slid
 

Similar to Perl6 for-beginners

Was können wir von Rebol lernen?
Was können wir von Rebol lernen?Was können wir von Rebol lernen?
Was können wir von Rebol lernen?lichtkind
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?lichtkind
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)FrescatiStory
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10acme
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 

Similar to Perl6 for-beginners (20)

Was können wir von Rebol lernen?
Was können wir von Rebol lernen?Was können wir von Rebol lernen?
Was können wir von Rebol lernen?
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?
 
Perl bhargav
Perl bhargavPerl bhargav
Perl bhargav
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
test ppt
test ppttest ppt
test ppt
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt21
ppt21ppt21
ppt21
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt17
ppt17ppt17
ppt17
 
ppt30
ppt30ppt30
ppt30
 

More from Jens Rehsack

Moo at System::Image::Update
Moo at System::Image::UpdateMoo at System::Image::Update
Moo at System::Image::UpdateJens Rehsack
 
A CMDB Driven by Perl
A CMDB Driven by PerlA CMDB Driven by Perl
A CMDB Driven by PerlJens Rehsack
 
PkgSrc in Five Minutes
PkgSrc in Five MinutesPkgSrc in Five Minutes
PkgSrc in Five MinutesJens Rehsack
 
Moo at App::Math::Trainer
Moo at App::Math::TrainerMoo at App::Math::Trainer
Moo at App::Math::TrainerJens Rehsack
 
Cross Compiling for Perl Hackers
Cross Compiling for Perl HackersCross Compiling for Perl Hackers
Cross Compiling for Perl HackersJens Rehsack
 
Perl on-embedded-devices
Perl on-embedded-devicesPerl on-embedded-devices
Perl on-embedded-devicesJens Rehsack
 

More from Jens Rehsack (7)

Unix::Statgrab
Unix::StatgrabUnix::Statgrab
Unix::Statgrab
 
Moo at System::Image::Update
Moo at System::Image::UpdateMoo at System::Image::Update
Moo at System::Image::Update
 
A CMDB Driven by Perl
A CMDB Driven by PerlA CMDB Driven by Perl
A CMDB Driven by Perl
 
PkgSrc in Five Minutes
PkgSrc in Five MinutesPkgSrc in Five Minutes
PkgSrc in Five Minutes
 
Moo at App::Math::Trainer
Moo at App::Math::TrainerMoo at App::Math::Trainer
Moo at App::Math::Trainer
 
Cross Compiling for Perl Hackers
Cross Compiling for Perl HackersCross Compiling for Perl Hackers
Cross Compiling for Perl Hackers
 
Perl on-embedded-devices
Perl on-embedded-devicesPerl on-embedded-devices
Perl on-embedded-devices
 

Recently uploaded

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 

Recently uploaded (20)

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
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