1 of 99

Introduction

to Perl 6

2014

2 of 99

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.

3 of 99

History

RFCs

Apocalypses

Exegesis

Synopses

Pugs

Roast

4 of 99

What about Perl 5?

  • Still here, regular releases

  • Separate teams

  • Cross-pollination

5 of 99

The Spec

The Synopses

http://perlcabal.org/syn/

The test suite

https://github.com/perl6/roast

6 of 99

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.

7 of 99

Rakudo

"Rakudo" is short for "Rakuda-dō" or Way of the Camel

Targets 3 VMs:

  • parrot (original)
  • JVM (portability, concurrency, slow startup)
  • MoarVM (decade of “what P6 needs”)

72 monthly releases, the latest of which is today!

8 of 99

NQP - Not Quite Perl 6

  • started as abstraction from parrot
  • subset of Perl 6 (needed to write a compiler!)
  • bootstrapped - it’s written in NQP.
  • rakudo itself is written almost entirely in Perl 6 and NQP (compare to Perl 5 guts)
  • already used NQP to bootstrap JVM and MoarVM.

9 of 99

Basic Compiler Architecture

Code

→ parsed to QAST (NQP)

→ transformed to VM-specific AST

→ JVM/MoarVM directly generate bytecode

→ parrot generates PIR (ASM), then bytecode

10 of 99

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");

11 of 99

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.

12 of 99

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>

13 of 99

Subroutine signatures - positional

sub vpn($ip, $token, $duration?) { … }

sub hello(Str $name="MaryAnn") { … }

sub collate(@arr1, @arr2, *@opts) { … }

14 of 99

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)

15 of 99

multis

multi odd-or-even(Int $i where * %% 2) {

say "even"

};�multi odd-or-even(Int $i) {

say "odd"

};�

16 of 99

Rules

  • a rule is just a sub
  • composable into grammars (a grammar is just a class)
  • can still write :P5
  • Perl 6’s grammar is written in Perl 6!
  • Grammars:Actions (:: lex:yacc)

17 of 99

OO - classes

class COG {

has @!inventory;

method checkout(Equipment $piece) {

so @!inventory.grep($piece)

}

}

18 of 99

OO - roles

role Meetings {

has @.meetings;

method create($name, $date) {

@.meetings.push($name => $date);

}

}

class COG does Meetings {...}

19 of 99

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)

20 of 99

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

21 of 99

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

22 of 99

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

23 of 99

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

24 of 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

25 of 99

custom operators

sub postfix:<!> { [*] 2..$^a }

say 100!

#93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 - Int

26 of 99

sub MAIN

sub MAIN(Int $age, Str $name) {

say "$name is $age years old.";

}

$ perl6 foo.p6 three Coke

Usage:

foo.p6 <age> <name>

27 of 99

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'

28 of 99

exceptions - try vs EVAL

try and EVAL are now separate

try { might-die } # check $! , or $!.WHAT

29 of 99

exceptions - typed exceptions

{

say 1/0;

CATCH {

when X::Numeric::DivideByZero {

say "eek"

}

}

} # anything else not caught

30 of 99

What’s next?

  • Increase adoption
    • Improve performance!
  • Freeze specification
  • Fix bugs
  • Docs
  • Improve P5 integration
  • Write modules

31 of 99

Performance

MOAR

32 of 99

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!

33 of 99

What’s where?

  • perl6.org - main site
  • rt.perl.org - rakudo bug tracker
  • doc.perl6.org
  • “CPAN” - modules.perl6.org

  • irc.freenode.net/#perl6

34 of 99

Introduction

to Perl 6

2014

35 of 99

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.

36 of 99

History

RFCs

Apocalypses

Exegesis

Synopses

Pugs

Roast

37 of 99

What about Perl 5?

  • Still here, regular releases

  • Separate teams

  • Cross-pollination

38 of 99

The Spec

The Synopses

http://perlcabal.org/syn/

The test suite

https://github.com/perl6/roast

39 of 99

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.

40 of 99

Rakudo

"Rakudo" is short for "Rakuda-dō" or Way of the Camel

Targets 3 VMs:

  • parrot (original)
  • JVM (portability, concurrency, slow startup)
  • MoarVM (decade of “what P6 needs”)

72 monthly releases, the latest of which is today!

41 of 99

NQP - Not Quite Perl 6

  • started as abstraction from parrot
  • subset of Perl 6 (needed to write a compiler!)
  • bootstrapped - it’s written in NQP.
  • rakudo itself is written almost entirely in Perl 6 and NQP (compare to Perl 5 guts)
  • already used NQP to bootstrap JVM and MoarVM.

42 of 99

Basic Compiler Architecture

Code

→ parsed to QAST (NQP)

→ transformed to VM-specific AST

→ JVM/MoarVM directly generate bytecode

→ parrot generates PIR (ASM), then bytecode

43 of 99

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");

44 of 99

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.

45 of 99

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>

46 of 99

Subroutine signatures - positional

sub vpn($ip, $token, $duration?) { … }

sub hello(Str $name="MaryAnn") { … }

sub collate(@arr1, @arr2, *@opts) { … }

47 of 99

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)

48 of 99

multis

multi odd-or-even(Int $i where * %% 2) {

say "even"

}�multi odd-or-even(Int $i) {

say "odd"

}�

49 of 99

Rules

  • a rule is just a sub
  • composable into grammars (a grammar is just a class)
  • can still write :P5
  • Perl 6’s grammar is written in Perl 6!
  • Grammars:Actions (:: lex:yacc)

50 of 99

OO - classes

class COG {

has @!inventory;

method checkout(Equipment $piece) {

so @!inventory.grep($piece)

}

}

51 of 99

OO - roles

role Meetings {

has @.meetings;

method create($name, $name) {

@.meetings.push($name => $name);

}

}

class COG does Meetings { … }

52 of 99

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)

53 of 99

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

54 of 99

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

55 of 99

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

56 of 99

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

57 of 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

58 of 99

custom operators

sub postfix:<!> { [*] 2..$^a }

say 100!

#93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 - Int

59 of 99

sub MAIN

sub MAIN(Int $age, Str $name) {

say "$name is $age years old.";

}

$ perl6 foo.p6 three Coke

Usage:

foo.p6 <age> <name>

60 of 99

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'

61 of 99

exceptions - try vs EVAL

try and EVAL are now separate

try { might-die } # check $! , or $!.WHAT

62 of 99

exceptions - typed exceptions

{

say 1/0;

CATCH {

when X::Numeric::DivideByZero {

say "eek"

}

}

} # anything else not caught

63 of 99

What’s next?

  • Increase adoption
    • Improve performance!
  • Freeze specification
  • Fix bugs
  • Docs
  • Improve P5 integration
  • Write modules

64 of 99

Performance

MOAR

65 of 99

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!

66 of 99

What’s where?

  • perl6.org - main site
  • rt.perl.org - rakudo bug tracker
  • doc.perl6.org
  • “CPAN” - modules.perl6.org

  • irc.freenode.net/#perl6

67 of 99

Introduction

to Perl 6

2014

68 of 99

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.

69 of 99

History

RFCs

Apocalypses

Exegesis

Synopses

Pugs

Roast

70 of 99

What about Perl 5?

  • Still here, regular releases

  • Separate teams

  • Cross-pollination

71 of 99

The Spec

The Synopses

http://perlcabal.org/syn/

The test suite

https://github.com/perl6/roast

72 of 99

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.

73 of 99

Rakudo

"Rakudo" is short for "Rakuda-dō" or Way of the Camel

Targets 3 VMs:

  • parrot (original)
  • JVM (portability, concurrency, slow startup)
  • MoarVM (decade of “what P6 needs”)

72 monthly releases, the latest of which is today!

74 of 99

NQP - Not Quite Perl 6

  • started as abstraction from parrot
  • subset of Perl 6 (needed to write a compiler!)
  • bootstrapped - it’s written in NQP.
  • rakudo itself is written almost entirely in Perl 6 and NQP (compare to Perl 5 guts)
  • already used NQP to bootstrap JVM and MoarVM.

75 of 99

Basic Compiler Architecture

Code

→ parsed to QAST (NQP)

→ transformed to VM-specific AST

→ JVM/MoarVM directly generate bytecode

→ parrot generates PIR (ASM), then bytecode

76 of 99

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");

77 of 99

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.

78 of 99

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>

79 of 99

Subroutine signatures - positional

sub vpn($ip, $token, $duration?) { … }

sub hello(Str $name="MaryAnn") { … }

sub collate(@arr1, @arr2, *@opts) { … }

80 of 99

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)

81 of 99

multis

multi odd-or-even(Int $i where * %% 2) {

say "even"

}�multi odd-or-even(Int $i) {

say "odd"

}�

82 of 99

Rules

  • a rule is just a sub
  • composable into grammars (a grammar is just a class)
  • can still write :P5
  • Perl 6’s grammar is written in Perl 6!
  • Grammars:Actions (:: lex:yacc)

83 of 99

OO - classes

class COG {

has @!inventory;

method checkout(Equipment $piece) {

so @!inventory.grep($piece)

}

}

84 of 99

OO - roles

role Meetings {

has @.meetings;

method create($name, $name) {

@.meetings.push($name => $name);

}

}

class COG does Meetings { … }

85 of 99

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)

86 of 99

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

87 of 99

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

88 of 99

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

89 of 99

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

90 of 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

91 of 99

custom operators

sub postfix:<!> { [*] 2..$^a }

say 100!

#93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 - Int

92 of 99

sub MAIN

sub MAIN(Int $age, Str $name) {

say "$name is $age years old.";

}

$ perl6 foo.p6 three Coke

Usage:

foo.p6 <age> <name>

93 of 99

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'

94 of 99

exceptions - try vs EVAL

try and EVAL are now separate

try { might-die } # check $! , or $!.WHAT

95 of 99

exceptions - typed exceptions

{

say 1/0;

CATCH {

when X::Numeric::DivideByZero {

say "eek"

}

}

} # anything else not caught

96 of 99

What’s next?

  • Increase adoption
    • Improve performance!
  • Freeze specification
  • Fix bugs
  • Docs
  • Improve P5 integration
  • Write modules

97 of 99

Performance

MOAR

98 of 99

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!

99 of 99

What’s where?

  • perl6.org - main site
  • rt.perl.org - rakudo bug tracker
  • doc.perl6.org
  • “CPAN” - modules.perl6.org

  • irc.freenode.net/#perl6