Download - $@%!»; – Perl 6
$@%!»; – Perl 6
Frank Blendinger
2013-10-21
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a
letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
204/20/23 / Method Park Software AG
Disclaimer
304/20/23 / Method Park Software AG
6 != 5
»The Perl 6 design process is about keeping what works in Perl 5, fixing what doesn't, and adding what's missing. That means there will be a few fundamental changes to the language, a large number of extensions to existing features, and a handful of completely new ideas. These modifications, enhancements, and innovations will work together to make the future Perl even more insanely great – without, we hope, making it even more greatly insane.«
Damian Conway
404/20/23 / Method Park Software AG
Perl Philosophy
TIMTOWTDO: There Is More Than One Way To Do It
DWIM: Do What I Mean
Keep easy things easy and hard things possible
slurp("filename").lines
Huffman coding Common things → short name Crazy stuff → long names “Weird things should look weird”
504/20/23 / Method Park Software AG
A Little Perl History
1987: Perl 1.0, 1994: Perl 5.0, 2013-05-18: Perl 5.18
2000: Perl 6 project started
New language – no compatibility with Perl 5
use v6; *.pl/.pm vs. *.p6
Intended for friendly coexistence with Perl 5 (still developed)
Some Perl 6 features backported to Perl 5(say, given/when, ~~, various CPAN modules: Perl6::*)
604/20/23 / Method Park Software AG
Perl 6: Community Driven Development
»Perl 5 was my rewrite of Perl. I want Perl 6 to be the community's rewrite of Perl and of the community.«
Larry Wall
704/20/23 / Method Park Software AG
Perl 6 Language Specification
http://perlcabal.org/syn/
804/20/23 / Method Park Software AG
Perl 6 Language Specification
https://github.com/perl6/specs
904/20/23 / Method Park Software AG
Perl 6 Language Specification
1004/20/23 / Method Park Software AG
Perl 6 Implementations
»Perl 6 is anything that passes the official test suite.«
Larry Wall, “Synopsis 1: Overview”
1104/20/23 / Method Park Software AG
Perl 6 Compilers / Interpreters / VMs
Rakudo Parrot VM backend JVM backend
Niecza compiles to CLR (.NET/Mono)
Pugs one of the first Perl 6 compilers no longer maintained
STD Larry Wall’s reference implementation
1204/20/23 / Method Park Software AG
Release Date: Christmas
Rakudo and Niecza are pretty closehttp://perl6.org/compilers/features
Want to play with Perl 6 right now? Use Rakudo *!
Rakudo * = Rakudo Perl 6 compiler, debugger, modules+ Parrot VM
Windows installer, Debian package, cygwin, tarball
PATH=$PATH:…/rakudo/binperl6 helloworld.p6
1304/20/23 / Method Park Software AG
Remove Inconsistencies
»In Perl 6, we decided it would be better to fix the language than fix the user.«
Larry Wall
1404/20/23 / Method Park Software AG
Perl 5
#!/usr/bin/env perl use strict; use warnings;
…
1504/20/23 / Method Park Software AG
Perl 6
#!/usr/bin/env perl6
use v6;
# implicit strict & warnings pragmas# Can be turned off with:# no strict;# no warnings;# but you don’t want to!
1604/20/23 / Method Park Software AG
Sigil Invariance
Perl 5
my $scalar = "sclr";
my @array = (1, 2, 3);
my %hash = (foo => 23, bar => 42);
1704/20/23 / Method Park Software AG
Sigil Invariance
Perl 5
my $scalar = "sclr"; my @array = (1, 2, 3); my %hash = (foo => 23, bar => 42); print $scalar . "\n"; print @array[0] . "\n"; # works, but warning print %hash{'foo'} . "\n"; # syntax error
1804/20/23 / Method Park Software AG
Sigil Invariance
Perl 5
my $scalar = "sclr"; my @array = (1, 2, 3); my %hash = (foo => 23, bar => 42); print $scalar . "\n"; print $array[1] . "\n"; print $hash{'bar'} . "\n";
1904/20/23 / Method Park Software AG
Sigil Invariance
Perl 6
my $scalar = "sclr";
my @array = (1, 2, 3);
my %hash = (foo => 23, bar => 42);
print $scalar ~ "\n";
print @array[0] ~ "\n";
print %hash{'foo'} ~ "\n";
2004/20/23 / Method Park Software AG
Sigil Invariance
Perl 6
my $scalar = "sclr"; my @array = (1, 2, 3); my %hash = (foo => 23, bar => 42); print $scalar ~ "\n"; print @array[0] ~ "\n"; print %hash{'foo'} ~ "\n"; print $array[1] ~ "\n";# Variable '$array' is not declared. # Did you mean '@array'?
2104/20/23 / Method Park Software AG
Sigil Invariance
Perl 6
my $scalar = "sclr";
my @array = (1, 2, 3);
my %hash = (foo => 23, bar => 42);
print $scalar ~ "\n";
print @array[0] ~ "\n";
print %hash{'foo'} ~ "\n";
2204/20/23 / Method Park Software AG
Sigil Invariance
Perl 6
my $scalar = "sclr"; my @array = 1, 2, 3; my %hash = foo => 23, bar => 42; say $scalar; say @array[0]; say %hash{'foo'};
2304/20/23 / Method Park Software AG
Sigil Invariance
Perl 6
my $scalar = "sclr"; my @array = 1, 2, 3; my %hash = foo => 23, bar => 42; say $scalar; say @array[0]; say %hash<foo>;
2404/20/23 / Method Park Software AG
Sigil Invariance
# Perl 5: get list from hash of hashes
my @verbs = @{ $dict { 'verb' }{ 'transitive' } };
# Perl 6: get list from hash of hashesmy @verbs = %dict{ 'verb' }{ 'transitive' };
# Perl 6: or even like thismy @verbs = %dict<verb><transitive>;
2504/20/23 / Method Park Software AG
Perl 6 Basics
2604/20/23 / Method Park Software AG
Subroutines
sub square-and-sum($x, $y) { return $x*$x + $y*$y; } my $a = 3; my $b = 4; say "{$a}² + {$b}² = {square-and-sum($a, $b)}";# 3² + 4² = 25
2704/20/23 / Method Park Software AG
Subroutines
sub stutter($word) {
my $first-letter = substr($word, 0, 1); my $wwwword = ($first-letter ~ "-") x 3 ~ $word;
say $wwwword;
}
stutter("what"); # w-w-w-what
2804/20/23 / Method Park Software AG
Subroutines
sub stutterify($word) { my $first-letter = substr($word, 0, 1);
my $wwwword = ($first-letter ~ "-") x 3 ~
$word; $word = $wwwword; # no-no! $word is read-
only! }
2904/20/23 / Method Park Software AG
Subroutines
sub stutterify($word is rw) { my $first-letter = substr($word, 0, 1); my $wwwword = ($first-letter ~ "-") x 3 ~ $word; $word = $wwwword; }
3004/20/23 / Method Park Software AG
Subroutines
sub stutterify($word is rw) { my $first-letter = substr($word, 0, 1); my $wwwword = ($first-letter ~ "-") x 3 ~ $word; $word = $wwwword; }
my $word = "hello"; $word.say; # hello stutterify($word); $word.say; # h-h-h-hello
3104/20/23 / Method Park Software AG
Subroutines
# pass-by-valuesub say-next-num($num is copy) { say ++$num; } my $num-of-truth = 42; say-next-num($num-of-truth); say "The truth is still $num-of-truth";
3204/20/23 / Method Park Software AG
Dealing with Arrays
sub shout-words(@words) { for @words -> $word { say uc($word); } }
3304/20/23 / Method Park Software AG
Dealing with Arrays
sub shout-words(@words) { for @words { say uc($_); # topic variable
} }
3404/20/23 / Method Park Software AG
Dealing with Arrays
sub shout-words(@words) { for @words { say uc($_) }} my @words = <hey ho let's go>; shout-words(@words);
# HEY# HO# LET'S# GO
3504/20/23 / Method Park Software AG
Dealing with Hashes
sub say-names-with-age(%people) {
for %people.kv -> $key, $value {
say $key ~ " (" ~ $value ~ ")";
}
}
3604/20/23 / Method Park Software AG
Dealing with Hashes
sub say-names-with-age(%people) {
for %people {
say $_.key ~ " (" ~ $_.value ~ ")";
}}
3704/20/23 / Method Park Software AG
Dealing with Hashes
sub say-names-with-age(%people) {
for %people {
say .key ~ " (" ~ .value ~ ")";
}
}
3804/20/23 / Method Park Software AG
Dealing with Hashes
sub say-names-with-age(%people) { for %people { say .key ~ " (" ~ .value ~ ")"; } }
my %white-family = Walter => 52, Skyler => 41, "Walter Jr." => 17, Holly => 2; say-names-with-age(%white-family);
3904/20/23 / Method Park Software AG
Optional Parameters
sub whatever($optional?) { if defined($optional) { say "$optional is pretty cool."; } else { say "Whatever, man."; } }
whatever("The dude"); whatever();
4004/20/23 / Method Park Software AG
Named Parameters
sub protip(:$what, :$who) { say "Use $what, $who!"; } protip(:what("the force"), :who("Luke"));protip(:who("Luke"), :what("the force"));
4104/20/23 / Method Park Software AG
Named Parameters
sub protip(:$what, :$who) { say "Use $what, $who!"; } # short form for strings protip(:who<Luke>, :what<the force>);
4204/20/23 / Method Park Software AG
Named Parameters
sub protip(:$what, :$who) { say "Use $what, $who!"; } my $thing = "the force";
my $guy = "Luke";
#protip( $thing, $guy); # Nope. #protip(:$thing, :$guy); # Also, nope.protip(:what($thing), :who($guy));
4304/20/23 / Method Park Software AG
Named Parameters
sub protip(:$what, :$who) { say "Use $what, $who!"; } my $what = "the force";
my $who = "Luke";
protip(:$who, :$what); # names match → magic!
4404/20/23 / Method Park Software AG
Required Named Parameters
sub protip(:$what, :$who) { say "Use $what, $who!"; } # named params are optional by defaultprotip();# use of uninitialized value of type …
4504/20/23 / Method Park Software AG
Required Named Parameters
sub protip(:$what!, :$who!) { say "Use $what, $who!"; } # named params are optional by defaultprotip();# Required named parameter 'what' not passed
4604/20/23 / Method Park Software AG
Varargs with Slurpy Arrays
# varargs with slurpy arrays (* prefix) sub sum(*@nums) { [+] @nums; } say sum 42, 23, 1337;
4704/20/23 / Method Park Software AG
Varargs with Slurpy Arrays
# default (no "()" given!) is *@_, so:
sub sum {
[+] @_;
}
say sum 42, 23, 1337;
4804/20/23 / Method Park Software AG
Procedural vs. Object Oriented
Perl 6 is object-oriented at its core
You can use it, but you don’t have to (TIMTOWTDI!)
4904/20/23 / Method Park Software AG
Procedural vs. Object Oriented
Perl 6 is object-oriented at its core
You can use it, but you don’t have to (TIMTOWTDI!)
# proceduralsay split(" ", capitalize("hello world"))[1]; # World
5004/20/23 / Method Park Software AG
Procedural vs. Object Oriented
Perl 6 is object-oriented at its core
You can use it, but you don’t have to (TIMTOWTDI!)
# proceduralsay split(" ", capitalize("hello world"))[1]; # World
# object oriented"hello world".capitalize.split(" ")[1].say;# World
5104/20/23 / Method Park Software AG
Objects
Everything is an object
my $x = "foo"; # Str objectmy $y = 42; # Int objectmy $z = Int; # Int class
5204/20/23 / Method Park Software AG
Objects
Everything is an object
my $x = "foo"; # Str objectmy $y = 42; # Int objectmy $z = Int; # Int class
my &code = { say "Ohai." };&code();
5304/20/23 / Method Park Software AG
Objects
Everything is an object
my $x = "foo"; # Str objectmy $y = 42; # Int objectmy $z = Int; # Int class
my &code = { say "Ohai." };&code(); &code = sub ($name) { say "Ohai {$name}." }; &code("Alice");
5404/20/23 / Method Park Software AG
Introspection
What are you?
42.WHAT; # (Int)(1/2).WHAT; # (Rat)
5504/20/23 / Method Park Software AG
Introspection
What are you?
42.WHAT; # (Int)(1/2).WHAT; # (Rat)
What can you do for me?
42.^methods(); # Int Num Rat FatRat abs Bridge chr # sqrt base expmod is-prime floor # ceiling round lsb msb sign conj
rand # sin …
5604/20/23 / Method Park Software AG
Type Objects vs. Instance Objects
my $i = Int;
say $i.WHAT; # (Int)
say $i.defined; # False
say $i; # (Int)
$i = Int.new; # or: $i = $i.new or: $i .= new; say $i.WHAT; # (Int)
say $i.defined; # True
say $i; # 0
5704/20/23 / Method Park Software AG
Perlify!
my %complicatedObj = Hash.new;
%complicatedObj{'string'} = "this is foo";
%complicatedObj{'list'} = [4, 5, 6];
%complicatedObj{'parcel'} = 4, 5, 6;%complicatedObj{'range'} = 10..15;
%complicatedObj{'inner hash'} = x => 1.1, y => 2.5;
%complicatedObj.perl.say;
# ("string" => "this is foo", "list" => [4, 5, 6],"parcel" => $(4, 5, 6), "range" => 10..15,"inner hash" => $("x" => 1.1, "y" => 2.5)).hash
5804/20/23 / Method Park Software AG
We Have to Go Deeper!
5904/20/23 / Method Park Software AG
DUMP
my %complicatedObj = Hash.new;
%complicatedObj{'string'} = "this is foo";
%complicatedObj{'list'} = [4, 5, 6];
%complicatedObj{'parcel'} = 4, 5, 6;%complicatedObj{'range'} = 10..15;
%complicatedObj{'inner hash'} = x => 1.1, y =>
2.5;
%complicatedObj.DUMP.say;
6004/20/23 / Method Park Software AG
DUMP
Hash<1>( :$!descriptor(Perl6::Metamodel::ContainerDescriptor<2>(...)), :$!storage(Hash<3>( string => ▶"this is foo", list => ▶Array<5>( :$!flattens(True), :$!items(QRPA<7>( ▶4, ▶5, ▶6 )), :$!nextiter(▶Mu) ), parcel => ▶Parcel<12>(:$!storage(RPA<13>( =Int<8>, =Int<9>, =Int<10> ))),
range => ▶Range<14>( :min(▶10), :max(▶15), :excludes_min(▶False), :excludes_max(▶False) ), inner hash => ▶Parcel<18>(:$!storage(RPA<19>( Pair<20>( :key(▶"x"), :value(▶1.1) ), Pair<23>( :key(▶"y"), :value(▶2.5) ) ))) )))
6104/20/23 / Method Park Software AG
DUMP
Hash<1>( :$!descriptor(Perl6::Metamodel::ContainerDescriptor<2>(...)), :$!storage(Hash<3>( string => ▶"this is foo", list => ▶Array<5>( :$!flattens(True), :$!items(QRPA<7>( ▶4, ▶5, ▶6 )), :$!nextiter(▶Mu) ), parcel => ▶Parcel<12>(:$!storage(RPA<13>( =Int<8>, =Int<9>, =Int<10> ))),
range => ▶Range<14>( :min(▶10), :max(▶15), :excludes_min(▶False), :excludes_max(▶False) ), inner hash => ▶Parcel<18>(:$!storage(RPA<19>( Pair<20>( :key(▶"x"), :value(▶1.1) ), Pair<23>( :key(▶"y"), :value(▶2.5) ) ))) )))
6204/20/23 / Method Park Software AG
Type System: Dynamic Types
my @untypedArray; @untypedArray.push(23).push("foo").push([1, 2]); @untypedArray.perl.say;# Array.new(23, "foo", [1, 2])
6304/20/23 / Method Park Software AG
Type System: Static Types
my @untypedArray; @untypedArray.push(23).push("foo").push([1, 2]); @untypedArray.perl.say;# Array.new(23, "foo", [1, 2])
my Int @intArray; @intArray.push(23).push("foo");# Type check failed in .push; expected 'Int' but
got 'Str'
6404/20/23 / Method Park Software AG
Beyond Type Safety: Contraints
sub div(Real $x, Real $y where $y != 0) { $x / $y };
div(1, 2).say;# 0.5 div(1, 0).say;# Constraint type check failed for parameter
'$y'
6504/20/23 / Method Park Software AG
Default Parameters
sub draw-ascii-rectangle(Int $width where 1..10 = 2, Int $height where 1..10 = $width) { for ^$height { for ^$width { print "#"; } print "\n"; } }
6604/20/23 / Method Park Software AG
Default Parameters
sub draw-ascii-rectangle(Int $width where 1..10 = 2, Int $height where 1..10 = $width) { for ^$height { for ^$width { print "#"; } print "\n"; } } draw-ascii-rectangle(10, 1); draw-ascii-rectangle(); draw-ascii-rectangle(4);
6704/20/23 / Method Park Software AG
Default Parameters
sub draw-ascii-rectangle(Int $width where 1..10 = 2, Int $height where 1..10 = $width) { for ^$height { for ^$width { print "#"; } print "\n"; } } draw-ascii-rectangle(10, 1); draw-ascii-rectangle(); draw-ascii-rectangle(4);
##########
##
##
####
####
####
####
6804/20/23 / Method Park Software AG
Object Oriented Programming
6904/20/23 / Method Park Software AG
Classes
class Point { has $!x; # private has $!y; # private
method to-string() { "[$!x/$!y]"; } } my $point = Point.new;
7004/20/23 / Method Park Software AG
Classes
class Point { has $!x; # private has $!y; # private method to-string() { "[$!x/$!y]"; } } my $point = Point.new; $point.to-string().say; # use of unit. value…
#$point.x.say; # no access, private $point.DUMP.say; # Point<1>(:$!x(Any), :$!
y(Any))
7104/20/23 / Method Park Software AG
Classes
class Point { has $.x; # public has $.y; # public
method to-string() { "[$!x/$!y]"; } } my $point = Point.new(:x(1), :y(2));
$point.to-string().say; # [1/2]
7204/20/23 / Method Park Software AG
Classes
class Point { has $.x; # public has $.y; # public
method to-string() { "[$!x/$!y]"; } } my $point = Point.new(x => 1, y => 2);
$point.to-string().say; # [1/2]
7304/20/23 / Method Park Software AG
Attributes vs. Accessor Methods
class Foo { has $.bar; method bar { $!bar * 10 } method use-attribute { say "bar is $!bar" } method use-accessor { say "bar is $.bar" }} Foo.new(bar => 10).use-attribute; # bar is 10
Foo.new(bar => 10).use-accessor; # bar is 100
7404/20/23 / Method Park Software AG
Read/Write Attributes
class Point { has $.x; # public has $.y; # public
method to-string() { "[$!x/$!y]"; } } my $point = Point.new(x => 1, y => 2);
$point.x = 23; # NOPE! $!x is readonly
7504/20/23 / Method Park Software AG
Read/Write Attributes
class Point { has $.x is rw; has $.y is rw;
method to-string() { "[$!x/$!y]"; } } my $point = Point.new(x => 1, y => 2); $point.x = 23;$point.y = 42;
$point.to-string().say; # [23/42]
7604/20/23 / Method Park Software AG
Default Values for Attributes
class Point { has $.x = 0; has $.y = $!x;
method to-string() { "[$!x/$!y]"; } } Point.new.to-string.say; # [0/0]Point.new(x => 9).to-string.say; # [9/9]
7704/20/23 / Method Park Software AG
Inheritance
class Mother { method i-am { say "I am your mother" } }
class Father { method i-am { say "I am your father" } }
class Child is Father is Mother {}
Child.new.i-am;
7804/20/23 / Method Park Software AG
Inheritance
class Mother { method i-am { say "I am your mother" } }
class Father { method i-am { say "I am your father" } }
class Child is Father is Mother {}
Child.new.i-am; # I am your father
Child.^mro.say; # (Child) (Father) (Mother) (Any)
(Mu)
7904/20/23 / Method Park Software AG
Inheritance – Private vs. Public Methods
class Dog { method !do-private { say "This is dog." } method do-public { self!do-private } } class Pug is Dog { method access-dog-public { self.do-public } method access-dog-private { self!do-private }} Dog.new.do-public;
8004/20/23 / Method Park Software AG
Inheritance – Private vs. Public Methods
class Dog { method !do-private { say "This is dog." } method do-public { self!do-private } } class Pug is Dog { method access-dog-public { self.do-public } method access-dog-private { self!do-private }} Dog.new.do-public; # ok Pug.new.do-public;
8104/20/23 / Method Park Software AG
Inheritance – Private vs. Public Methods
class Dog { method !do-private { say "This is dog." } method do-public { self!do-private } } class Pug is Dog { method access-dog-public { self.do-public } method access-dog-private { self!do-private }} Dog.new.do-public; # ok Pug.new.do-public; # ok Pug.new.access-dog-public;
8204/20/23 / Method Park Software AG
Inheritance – Private vs. Public Methods
class Dog { method !do-private { say "This is dog." } method do-public { self!do-private } } class Pug is Dog { method access-dog-public { self.do-public } method access-dog-private { self!do-private }} Dog.new.do-public; # ok Pug.new.do-public; # ok Pug.new.access-dog-public; # ok Pug.new.access-dog-private;
8304/20/23 / Method Park Software AG
Inheritance – Private vs. Public Methods
class Dog { method !do-private { say "This is dog." } method do-public { self!do-private } } class Pug is Dog { method access-dog-public { self.do-public } method access-dog-private { self!do-private }} Dog.new.do-public; # ok Pug.new.do-public; # ok Pug.new.access-dog-public; # ok Pug.new.access-dog-private; # NOPE
8404/20/23 / Method Park Software AG
Roles
role Happy { method have-fun { say ":-)" } } role Sad { method frown { say ":-/" } } class Guy does Happy does Sad { method code-some-perl6 { self.have-fun } method do-powerpoint-pres { self.frown } }
8504/20/23 / Method Park Software AG
Roles
role Happy { method have-fun { say ":-)" } } role Sad { method frown { say ":-/" } } class Guy does Happy does Sad { method code-some-perl6 { self.have-fun } method do-powerpoint-pres { self.frown } } my $me = Guy.new; $me.code-some-perl6; # :-) $me.do-powerpoint-pres; # :-/ $me.have-fun; # :-)
8604/20/23 / Method Park Software AG
Stubs
role AbstractSerializable { method serialize() { ... } }
8704/20/23 / Method Park Software AG
Stubs
role AbstractSerializable { method serialize() { ... } } # not allowed: class APoint does AbstractSerializable { has $.x; has $.y; } # -> Method 'serialize' must be implemented by APoint…
8804/20/23 / Method Park Software AG
Stubs
role AbstractSerializable { method serialize() { ... } } # not allowed: class APoint does AbstractSerializable { has $.x; has $.y; } # -> Method 'serialize' must be implemented by APoint… # this works: class SPoint does AbstractSerializable { has $.x; has $.y; method serialize() { "p($.x, $.y)" } }
8904/20/23 / Method Park Software AG
Functional Programming
9004/20/23 / Method Park Software AG
Closures
my @times = (); # Array.new
for 1..10 {
my $multiplicant = $_;
@times[$_] = sub ($a) { $a * $multiplicant };
}
9104/20/23 / Method Park Software AG
Closures
my @times = (); # Array.new for 1..10 { my $multiplicant = $_; @times[$_] = sub ($a) { $a * $multiplicant };} say @times[3](4); # 12 say @times[5](20); # 100 say @times[7](3); # 21
9204/20/23 / Method Park Software AG
Infinite Lists, Lazy Evaluation
my @all-the-integers = 0..Inf; for ^10 { say @all-the-integers[$_] }
9304/20/23 / Method Park Software AG
Infinite Lists, Lazy Evaluation
my @all-the-integers = 0..Inf; for ^10 { say @all-the-integers[$_] }
9404/20/23 / Method Park Software AG
map
9504/20/23 / Method Park Software AG
map
(1..10).map( { $_ * $_ } ).join(", ").say;
9604/20/23 / Method Park Software AG
map
(1..10).map( { $_ * $_ } ).join(", ").say;
# 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
9704/20/23 / Method Park Software AG
Z – the zip operator
9804/20/23 / Method Park Software AG
Z – the zip operator
my @zipped-array = zip (1, 2, 3), (10, 20, 30);
@zipped-array.join(", ").say;# 1, 10, 2, 20, 3, 30
9904/20/23 / Method Park Software AG
Z – the zip operator
my @zipped-array = (1, 2, 3) Z (10, 20, 30);
@zipped-array.join(", ").say;# 1, 10, 2, 20, 3, 30
10004/20/23 / Method Park Software AG
Z – the zip operator
Application: iterating two lists in parallel
sub are-pairwise-greater(@array1, @array2) { for @array1 Z @array2 -> $x, $y { return False if $x <= $y; } return True; }
10104/20/23 / Method Park Software AG
Z – the zip operator
Application: iterating two lists in parallel
sub are-pairwise-greater(@array1, @array2) { for @array1 Z @array2 -> $x, $y { return False if $x <= $y; } return True; } are-pairwise-greater(2..11, 1..10).say; # True are-pairwise-greater(2..11, 1..5).say; # True are-pairwise-greater(2..3, 1..15).say; # True are-pairwise-greater((3,3,3), (1,2,3)).say; #
False
10204/20/23 / Method Park Software AG
Meta Operators
10304/20/23 / Method Park Software AG
Meta Operators
Reduce meta operator: [op]
Infix operator → list operator
1 + $x + 5 + $y
[+] (1, $x, 5, $y)
10404/20/23 / Method Park Software AG
Meta Operators
# multiply all elements of @a my $prod = [*] @a; # calculate mean of @a my $mean = ([+] @a) / @a.elems;
# true if elements of @a are numerically sorted my $sorted = [<=] @a; # find the smallest element of @a and @b combinedmy $min = [min] @a, @b;
10504/20/23 / Method Park Software AG
The Hyper Meta Operators: » / «
10604/20/23 / Method Park Software AG
The Hyper Operators: » / «
Sum of two lists
my @a = 1..10; my @b = 11..20;
my @sums = (); for @a Z @b -> $x, $y { @sums.push($x + $y);} @sums.join(", ").say;
10704/20/23 / Method Park Software AG
The Hyper Operators: » / «
Sum of two lists
my @a = 1..10; my @b = 11..20;
(@a »+« @b).join(", ").say;
10804/20/23 / Method Park Software AG
The Hyper Operators: » / «
Sum of two lists
my @a = 1..10; my @b = 11..20;
(@a »+« @b).join(", ").say;
# >>+<< works too.# But it’s not that cool.
10904/20/23 / Method Park Software AG
The Hyper Operators: » / «
# increment all elements @a = 1, 7, 9; @a»++; @a.join(", ").say; # get pairwise minimum @a = 1, 7, 9; @b = 4, 2, 9; my @minima = @a »min« @b; @minima.join(", ").say;
11004/20/23 / Method Park Software AG
Multi
11104/20/23 / Method Park Software AG
Multi
class Human { has $.name }
class CodeMonkey is Human {}
class BusinessGuy is Human {}
multi sub greet(Human $h) { say "Hello {$h.name}." }
multi sub greet(CodeMonkey $h) { say "Hey {$h.name}, sup dude?" }
multi sub greet(BusinessGuy $h) { say "Nice to meet you, {$h.name}."}
11204/20/23 / Method Park Software AG
Multi
class Human { has $.name }
class CodeMonkey is Human {}
class BusinessGuy is Human {}
multi sub greet(Human $h) { say "Hello {$h.name}." }
multi sub greet(CodeMonkey $h) { say "Hey {$h.name}, sup dude?" }
multi sub greet(BusinessGuy $h) { say "Nice to meet you, {$h.name}."}
my $john = CodeMonkey.new(name => "John Johnson");
my $jack = BusinessGuy.new(name => "Jack Jackson");
greet $john; # Hey John Johnson, sup dude?
greet $jack; # Nice to meet you, Jack Jackson.
11304/20/23 / Method Park Software AG
Multi with Constraints
multi sub mysign(Int $x where $x == 0) { 0 } multi sub mysign(Int $x where $x < 0) { -1 } multi sub mysign(Int $x) { +1 } for -2, 0, 2 { say mysign($_) }# -1# 0# 1
11404/20/23 / Method Park Software AG
Command Line Parsing with multi MAIN
multi sub MAIN('add‚ , $x, $y) { say "$x + $y = {$x + $y}" }multi sub MAIN('sub', $x, $y) { say "$x - $y = {$x - $y}" }multi sub MAIN('mult', $x, $y) { say "$x * $y = {$x * $y}" }multi sub MAIN('div', $x, $y) { say "$x / $y = {$x / $y}" }
│% ./cmdline_args.p6│Usage:│ ./cmdline_args.p6 add <x> <y>│ ./cmdline_args.p6 sub <x> <y>│ ./cmdline_args.p6 mult <x> <y>│ ./cmdline_args.p6 div <x> <y>
11504/20/23 / Method Park Software AG
Command Line Parsing with multi MAIN
multi sub MAIN('add‚ , $x, $y) { say "$x + $y = {$x + $y}" }multi sub MAIN('sub', $x, $y) { say "$x - $y = {$x - $y}" }multi sub MAIN('mult', $x, $y) { say "$x * $y = {$x * $y}" }multi sub MAIN('div', $x, $y) { say "$x / $y = {$x / $y}" }
│% ./cmdline_args.p6 add 23 42│23 + 42 = 65
│% ./cmdline_args.p6 mult 23 42│23 * 42 = 966
11604/20/23 / Method Park Software AG
given/when
11704/20/23 / Method Park Software AG
given/when
my $thing = 17.23; given $thing { when Bool { say "yes/no" } when Real { say "number" } default { say $thing.WHAT } }
11804/20/23 / Method Park Software AG
given/when
my $n = 17; given $n { when $n < 10 { say "small" } when $n < 100 { say "medium" } default { say "big" } }
11904/20/23 / Method Park Software AG
given/when
my ($x, $y) = 12, 23; given $x, $y { when $x < $y { say "smaller" } when $x > $y { say "greater" } when $x == $y { say "same" } }
12004/20/23 / Method Park Software AG
Smart Matching with ~~
12104/20/23 / Method Park Software AG
Smart Matching with ~~
class A {} class B is A {} class C {} my $b = B.new; say "b is a A" if $b ~~ A; # yep. say "b is a B" if $b ~~ B; # yep. say "b is a C" if $b ~~ C; # certainly not
12204/20/23 / Method Park Software AG
Smart Matching with ~~
my $num = 23; say "num has two digits" if $num ~~ 10..99;
my @list1 = 1, 2, 3, 4; my @list2 = 1..4; say "list are identical" if @list1 ~~ @list2;
12304/20/23 / Method Park Software AG
Smart Matching with ~~
my @list = 1, 2, 3; say (1 ~~ any(@list)) ?? "yes" !! "nope" ; say (6 ~~ any(@list)) ?? "yes" !! "nope" ;
my $text = "sOme tExT hEre"; say "matches" if $text ~~ m:i/text/;
12404/20/23 / Method Park Software AG
One More Thing
Awesome new regex syntax
… and beyond: rules, tokens, grammars
It’s a parsing wonderland!
12504/20/23 / Method Park Software AG
One More Thing
Awesome new regex syntax
… and beyond: rules, tokens, grammars
It’s a parsing wonderland!
Maybe another talk,if anyone cares. :-)
12604/20/23 / Method Park Software AG
References
http://perl6.org/ #perl6 @ FreeNode
https://github.com/perl6
https://github.com/perl6/book/
http://perl6advent.wordpress.com/
http://perl6maven.com/
http://rakudo.org/
12704/20/23 / Method Park Software AG
Questions?