Introduction
to Perl 6
2014
What is Perl 6?
"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, State of the Onion speech, TPC4
A language in the Perl family.
History
RFCs
Apocalypses
Exegesis
Synopses
Pugs
Roast
What about Perl 5?
The Spec
The Synopses
http://perlcabal.org/syn/
The test suite
https://github.com/perl6/roast
Implementations
Any code that passes the “6.0” version of the spec tests can call itself Perl 6.
Several implementations over the years, but the one with the most momentum today is rakudo.
Rakudo
"Rakudo" is short for "Rakuda-dō" or Way of the Camel
Targets 3 VMs:
72 monthly releases, the latest of which is today!
NQP - Not Quite Perl 6
Basic Compiler Architecture
Code
→ parsed to QAST (NQP)
→ transformed to VM-specific AST
→ JVM/MoarVM directly generate bytecode
→ parrot generates PIR (ASM), then bytecode
5 to 6
Invariant sigils:
my @letters = <i r c>; @letters[1] # r
Everything can be treated as an object:
@letters.sort # c r i
Everything is (can be) typed:
my Int $age = 37; # not old
. is for method calls; ~ is concatenation.
Dog.bark("bow" ~ ' ' ~ "wow");
Types
In Perl 5, you have a limited # of types.
In Perl 6 there are dozens of types; See S32.
Numeric, IO, Containers, Callable, Exception;
Even the object meta-model uses core types: Attribute, Signature, Method, etc.
Types - Numeric Examples
(3+i).WHAT # (Complex)
(4/6).WHAT # (Rat)
(4/6).Rat # 0.666667
(412/632).perl # <103/158>
(8/4).narrow.WHAT # Int
my Int $a = 2**70 # 1180591620717411303424
my int $a = 2**70 # 0
pi.Rat #<355/113>
Subroutine signatures - positional
sub vpn($ip, $token, $duration?) { … }
sub hello(Str $name="MaryAnn") { … }
sub collate(@arr1, @arr2, *@opts) { … }
Subroutine signatures - named
sub doctor(:$number, :$prop) {� say "Doctor # $number had a $prop";�}
�doctor(:prop("cricket bat"), :number<5>);�my $prop = "fez";
my $number = 11;
doctor(:$prop, :$number)
multis
multi odd-or-even(Int $i where * %% 2) {
say "even"
};�multi odd-or-even(Int $i) {
say "odd"
};�
Rules
OO - classes
class COG {
has @!inventory;
method checkout(Equipment $piece) {
so @!inventory.grep($piece)
}
}
OO - roles
role Meetings {
has @.meetings;
method create($name, $date) {
@.meetings.push($name => $date);
}
}
class COG does Meetings {...}
OO - metamodel/introspection
Dynamically create classes and roles that work the same as if you had written them in Perl 6.
Introspect all objects:
my $thing = new Cog; say $thing.WHAT; # (COG)
say $thing.^methods; # checkout create meetings
say $thing.^mro # (COG) (Any) (Mu)
say 3.^mro # (Int) (Cool) (Any) (Mu)
lazy lists, series, ranges, whatever
my @a = 1..Inf; @a[^20] # 1, 2, … 20
constant @fibs := 1,1,*+*...*;
@fibs[0..20]; # 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
Reduction operators
[*] [<] [+] [\*] …
[+] 1..^11 #55
[*] 1..6 #720
[<=] 1,2,3,5,4 #False
[<=] 1,2,3,3,4,5 #True
[\*] 1..6 # 1, 2, 6, 24, 120, 720
hyper operators
(3,8,2,9,3,8) >>->> 1; # (2,7,1,8,2,7)�@array »+=» 42; # add 42 to each
Point the arrows where you need to extend the list.
(1..10) <<*<< (1..5) # 1 4 9 16 25
(1..10) >>*>> (1..5) # 1 4 9 16 25 6 14 24 36 50
cross
<a b> X~ 1,2 # 'a1', 'a2', 'b1', 'b2'
for (0,10...^100) X 0..^10 -> $ten,$one {
say $ten + $one;
} # 0, 1, 2, 3 … 99
other meta operators
!== # negation of ==
1 Rcmp 2 # same as 2 cmp 1
<a b> X~ 1,2 # 'a1', 'a2', 'b1', 'b2'
sub infix:<hi> { say "$^a greets $^b" }
"Will" hi "Joel"; # Will greets Joel
"Will" Rhi "Joel"; # Joel greets Will
custom operators
sub postfix:<!> { [*] 2..$^a }
say 100!
#93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 - Int
sub MAIN
sub MAIN(Int $age, Str $name) {
say "$name is $age years old.";
}
$ perl6 foo.p6 three Coke
Usage:
foo.p6 <age> <name>
enums/subsets
enum RGB <red green blue>;
if $thing == red { … }
subset divisible-by-three of Int where *%%3;
my divisible-by-three $bar = 99; # ok
my divisible-by-three $foo = 2; # Type check failed in assignment to '$foo'; expected 'divisible-by-three' but got 'Int'
exceptions - try vs EVAL
try and EVAL are now separate
try { might-die } # check $! , or $!.WHAT
exceptions - typed exceptions
{
say 1/0;
CATCH {
when X::Numeric::DivideByZero {
say "eek"
}
}
} # anything else not caught
What’s next?
Performance
MOAR
Community
Join us on #perl6 on irc.freenode.net
Everyone is very friendly and helpful.
There’s an evalbot you can play with.
They even helped me fix up this presentation at the last minute. big thanks to timotimo++ for colorizing everything!
What’s where?
Introduction
to Perl 6
2014
What is Perl 6?
"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, State of the Onion speech, TPC4
A language in the Perl family.
History
RFCs
Apocalypses
Exegesis
Synopses
Pugs
Roast
What about Perl 5?
The Spec
The Synopses
http://perlcabal.org/syn/
The test suite
https://github.com/perl6/roast
Implementations
Any code that passes the “6.0” version of the spec tests can call itself Perl 6.
Several implementations over the years, but the one with the most momentum today is rakudo.
Rakudo
"Rakudo" is short for "Rakuda-dō" or Way of the Camel
Targets 3 VMs:
72 monthly releases, the latest of which is today!
NQP - Not Quite Perl 6
Basic Compiler Architecture
Code
→ parsed to QAST (NQP)
→ transformed to VM-specific AST
→ JVM/MoarVM directly generate bytecode
→ parrot generates PIR (ASM), then bytecode
5 to 6
Invariant sigils:
my @letters = <i r c>; @letters[1] # r
Everything can be treated as an object:
@letters.sort # c r i
Everything is (can be) typed:
my Int $age = 37; # not old
. is for method calls; ~ is concatenation.
Dog.bark("bow" ~ ' ' ~ "wow");
Types
In Perl 5, you have a limited # of types.
In Perl 6 there are dozens of types; See S32.
Numeric, IO, Containers, Callable, Exception;
Even the object meta-model uses core types: Attribute, Signature, Method, etc.
Types - Numeric Examples
(3+i).WHAT # (Complex)
(4/6).WHAT # (Rat)
(4/6).Rat # 0.666667
(412/632).perl # <103/158>
(8/4).narrow.WHAT # Int
my Int $a = 2**70 # 1180591620717411303424
my int $a = 2**70 # 0
pi.Rat.perl # <355/113>
Subroutine signatures - positional
sub vpn($ip, $token, $duration?) { … }
sub hello(Str $name="MaryAnn") { … }
sub collate(@arr1, @arr2, *@opts) { … }
Subroutine signatures - named
sub doctor(:$number, :$prop) {� say "Doctor # $number had a $prop";�}
�doctor(:prop("cricket bat"), :number<5>);�my $prop = "fez";
my $number = 11;
doctor(:$prop, :$number)
multis
multi odd-or-even(Int $i where * %% 2) {
say "even"
}�multi odd-or-even(Int $i) {
say "odd"
}�
Rules
OO - classes
class COG {
has @!inventory;
method checkout(Equipment $piece) {
so @!inventory.grep($piece)
}
}
OO - roles
role Meetings {
has @.meetings;
method create($name, $name) {
@.meetings.push($name => $name);
}
}
class COG does Meetings { … }
OO - metamodel/introspection
Dynamically create classes and roles that work the same as if you had written them in Perl 6.
Introspect all objects:
my $thing = new Cog; say $thing.WHAT; # (COG)
say $thing.^methods; # checkout create meetings
say $thing.^mro # (COG) (Any) (Mu)
say 3.^mro # (Int) (Cool) (Any) (Mu)
lazy lists, series, ranges, whatever
my @a = 1..Inf; @a[^20] # 1, 2, … 20
constant @fibs := 1,1,*+*...*;
@fibs[0..20]; # 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
Reduction operators
[*] [<] [+] [\*] …
[+] 1..^11 # 55
[*] 1..6 # 720
[<=] 1,2,3,5,4 # False
[<=] 1,2,3,3,4,5 # True
[\*] 1..6 # 1, 2, 6, 24, 120, 720
hyper operators
(3,8,2,9,3,8) >>->> 1; # (2,7,1,8,2,7)�@array »+=» 42; # add 42 to each
Point the arrows where you need to extend the list.
(1..10) <<*<< (1..5) # 1 4 9 16 25
(1..10) >>*>> (1..5) # 1 4 9 16 25 6 14 24 36 50
cross
<a b> X~ 1,2 # 'a1', 'a2', 'b1', 'b2'
for (0,10...^100) X 0..^10 -> $ten,$one {
say $ten + $one;
} # 0, 1, 2, 3 … 99
other meta operators
!== # negation of ==
1 Rcmp 2 # same as 2 cmp 1
<a b> X~ 1,2 # 'a1', 'a2', 'b1', 'b2'
sub infix:<hi> { say "$^a greets $^b" }
"Will" hi "Joel"; # Will greets Joel
"Will" Rhi "Joel"; # Joel greets Will
custom operators
sub postfix:<!> { [*] 2..$^a }
say 100!
#93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 - Int
sub MAIN
sub MAIN(Int $age, Str $name) {
say "$name is $age years old.";
}
$ perl6 foo.p6 three Coke
Usage:
foo.p6 <age> <name>
enums/subsets
enum RGB <red green blue>;
if $thing == red { … }
subset divisible-by-three of Int where *%%3;
my divisible-by-three $bar = 99; # ok
my divisible-by-three $foo = 2; # Type check failed in assignment to '$foo'; expected 'divisible-by-three' but got 'Int'
exceptions - try vs EVAL
try and EVAL are now separate
try { might-die } # check $! , or $!.WHAT
exceptions - typed exceptions
{
say 1/0;
CATCH {
when X::Numeric::DivideByZero {
say "eek"
}
}
} # anything else not caught
What’s next?
Performance
MOAR
Community
Join us on #perl6 on irc.freenode.net
Everyone is very friendly and helpful.
There’s an evalbot you can play with.
They even helped me fix up this presentation at the last minute. big thanks to timotimo++ for colorizing everything!
What’s where?
Introduction
to Perl 6
2014
What is Perl 6?
"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, State of the Onion speech, TPC4
A language in the Perl family.
History
RFCs
Apocalypses
Exegesis
Synopses
Pugs
Roast
What about Perl 5?
The Spec
The Synopses
http://perlcabal.org/syn/
The test suite
https://github.com/perl6/roast
Implementations
Any code that passes the “6.0” version of the spec tests can call itself Perl 6.
Several implementations over the years, but the one with the most momentum today is rakudo.
Rakudo
"Rakudo" is short for "Rakuda-dō" or Way of the Camel
Targets 3 VMs:
72 monthly releases, the latest of which is today!
NQP - Not Quite Perl 6
Basic Compiler Architecture
Code
→ parsed to QAST (NQP)
→ transformed to VM-specific AST
→ JVM/MoarVM directly generate bytecode
→ parrot generates PIR (ASM), then bytecode
5 to 6
Invariant sigils:
my @letters = <i r c>; @letters[1] # r
Everything can be treated as an object:
@letters.sort # c r i
Everything is (can be) typed:
my Int $age = 37; # not old
. is for method calls; ~ is concatenation.
Dog.bark("bow" ~ ' ' ~ "wow");
Types
In Perl 5, you have a limited # of types.
In Perl 6 there are dozens of types; See S32.
Numeric, IO, Containers, Callable, Exception;
Even the object meta-model uses core types: Attribute, Signature, Method, etc.
Types - Numeric Examples
(3+i).WHAT # (Complex)
(4/6).WHAT # (Rat)
(4/6).Rat # 0.666667
(412/632).perl # <103/158>
(8/4).narrow.WHAT # Int
my Int $a = 2**70 # 1180591620717411303424
my int $a = 2**70 # 0
pi.Rat.perl # <355/113>
Subroutine signatures - positional
sub vpn($ip, $token, $duration?) { … }
sub hello(Str $name="MaryAnn") { … }
sub collate(@arr1, @arr2, *@opts) { … }
Subroutine signatures - named
sub doctor(:$number, :$prop) {� say "Doctor # $number had a $prop";�}
�doctor(:prop("cricket bat"), :number<5>);�my $prop = "fez";
my $number = 11;
doctor(:$prop, :$number)
multis
multi odd-or-even(Int $i where * %% 2) {
say "even"
}�multi odd-or-even(Int $i) {
say "odd"
}�
Rules
OO - classes
class COG {
has @!inventory;
method checkout(Equipment $piece) {
so @!inventory.grep($piece)
}
}
OO - roles
role Meetings {
has @.meetings;
method create($name, $name) {
@.meetings.push($name => $name);
}
}
class COG does Meetings { … }
OO - metamodel/introspection
Dynamically create classes and roles that work the same as if you had written them in Perl 6.
Introspect all objects:
my $thing = new Cog; say $thing.WHAT; # (COG)
say $thing.^methods; # checkout create meetings
say $thing.^mro # (COG) (Any) (Mu)
say 3.^mro # (Int) (Cool) (Any) (Mu)
lazy lists, series, ranges, whatever
my @a = 1..Inf; @a[^20] # 1, 2, … 20
constant @fibs := 1,1,*+*...*;
@fibs[0..20]; # 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
Reduction operators
[*] [<] [+] [\*] …
[+] 1..^11 # 55
[*] 1..6 # 720
[<=] 1,2,3,5,4 # False
[<=] 1,2,3,3,4,5 # True
[\*] 1..6 # 1, 2, 6, 24, 120, 720
hyper operators
(3,8,2,9,3,8) >>->> 1; # (2,7,1,8,2,7)�@array »+=» 42; # add 42 to each
Point the arrows where you need to extend the list.
(1..10) <<*<< (1..5) # 1 4 9 16 25
(1..10) >>*>> (1..5) # 1 4 9 16 25 6 14 24 36 50
cross
<a b> X~ 1,2 # 'a1', 'a2', 'b1', 'b2'
for (0,10...^100) X 0..^10 -> $ten,$one {
say $ten + $one;
} # 0, 1, 2, 3 … 99
other meta operators
!== # negation of ==
1 Rcmp 2 # same as 2 cmp 1
<a b> X~ 1,2 # 'a1', 'a2', 'b1', 'b2'
sub infix:<hi> { say "$^a greets $^b" }
"Will" hi "Joel"; # Will greets Joel
"Will" Rhi "Joel"; # Joel greets Will
custom operators
sub postfix:<!> { [*] 2..$^a }
say 100!
#93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 - Int
sub MAIN
sub MAIN(Int $age, Str $name) {
say "$name is $age years old.";
}
$ perl6 foo.p6 three Coke
Usage:
foo.p6 <age> <name>
enums/subsets
enum RGB <red green blue>;
if $thing == red { … }
subset divisible-by-three of Int where *%%3;
my divisible-by-three $bar = 99; # ok
my divisible-by-three $foo = 2; # Type check failed in assignment to '$foo'; expected 'divisible-by-three' but got 'Int'
exceptions - try vs EVAL
try and EVAL are now separate
try { might-die } # check $! , or $!.WHAT
exceptions - typed exceptions
{
say 1/0;
CATCH {
when X::Numeric::DivideByZero {
say "eek"
}
}
} # anything else not caught
What’s next?
Performance
MOAR
Community
Join us on #perl6 on irc.freenode.net
Everyone is very friendly and helpful.
There’s an evalbot you can play with.
They even helped me fix up this presentation at the last minute. big thanks to timotimo++ for colorizing everything!
What’s where?