Perl 6 Edward Higgins 2019-03-04 Perl 6 A language for the 21st - - PowerPoint PPT Presentation

perl 6
SMART_READER_LITE
LIVE PREVIEW

Perl 6 Edward Higgins 2019-03-04 Perl 6 A language for the 21st - - PowerPoint PPT Presentation

Perl 6 Edward Higgins 2019-03-04 Perl 6 A language for the 21st Century [ Originally presented in Vim, ] [ best viewed in a full-screen terminal at 70x20 characters ] In the beginning... In the beginning... C sh awk sed grep In the


slide-1
SLIDE 1

Perl 6

Edward Higgins 2019-03-04

slide-2
SLIDE 2

Perl 6 A language for the 21st Century [ Originally presented in Vim, ] [ best viewed in a full-screen terminal at 70x20 characters ]

slide-3
SLIDE 3

In the beginning...

slide-4
SLIDE 4

In the beginning... C sh awk sed grep

slide-5
SLIDE 5

In the beginning... C \ sh \ awk :=> Perl 1.0 sed / grep /

slide-6
SLIDE 6

(1987) Perl 1.0 (1988) Perl 2.0 (1989) Perl 3.0 (1991) Perl 4.0 (1994) Perl 5.0

slide-7
SLIDE 7

Perl 5 | Perl 6

  • -----------|--------------------------

| (2000) Perl 5.6 | Design process anounced | (2002) Perl 5.8 |

  • Dev. on Parrot VM

| (2009) Perl 5.10 |

  • Dev. moved to Rakudo VM

| (2015) Perl 5.22 | Perl 6.c (First stable release) | (2018) Perl 5.28 | Perl 6.d

slide-8
SLIDE 8

The Perl Philosophy Natural language inspired

  • Flexible, malleable, expressive
  • Dialects are okay
  • The language grows with you
  • Context is important
slide-9
SLIDE 9

The Perl Philosophy TIMTOWTDOI (There's more than one way to do it) DWIM (Do what I mean) Easy things should be easy, hard things should be possible Give the user enough rope to shoot themselves in the foot

slide-10
SLIDE 10

The Perl 6 Philosophy Easy things should stay easy, hard things should get easier No arbitray limits Similar things should look similar Visual distinctions are important

  • Ofun
slide-11
SLIDE 11

# Variables my $scalar = 3.14; my @array = (1, 2, 3); my %hash = first-name => "Larry", surname => "Wall" ;

slide-12
SLIDE 12

# Typed Variables my Rat $scalar = 3.14; my Int @array = (1, 2, 3); my Str %hash = first-name => "Larry", surname => "Wall" ;

slide-13
SLIDE 13

# More Variables my Num $scalar = pi; my Int @array = (1, 2, 3); my Str %hash; %hash<first-name> = "Larry"; %hash<surname> = "Wall" ;

slide-14
SLIDE 14

# Rational numbers say 0.1.Rat + 0.2.Rat == 0.3.Rat;

slide-15
SLIDE 15

# Floating point numbers say 0.1.Num + 0.2.Num == 0.3.Num;

slide-16
SLIDE 16

# Numbers? say 0.1 + 0.2 == 0.3;

slide-17
SLIDE 17

# if-then-else my $age = 3; if $age < 18 { say "You can't drink in the UK yet"; } elsif 18 <= $age <= 20 { say "You can't drink in the USA yet"; } else { say "You can drink (almost) anywhere!"; }

slide-18
SLIDE 18

# given-when my UInt $age = 19; given $age { when 0..17 { say "You can't drink in the UK yet" } when 18..20 { say "You can't drink in the USA yet" } default { say "You can drink (almost) anywhere!" } }

slide-19
SLIDE 19

# for loops for (0 ... 10) -> $x { print "$x "; }

slide-20
SLIDE 20

# for loops for (0 ...^ 10) -> $x { print "$x "; }

slide-21
SLIDE 21

# for loops for ^10 -> $x { print "$x "; }

slide-22
SLIDE 22

# Sequences my @integers = 1 ... *; say @integers[^20];

slide-23
SLIDE 23

# Sequences my @evens = 2, 4 ... *; say @evens[^10];

slide-24
SLIDE 24

# Sequences my @powers = 2, {2 * $^x} ... *; say @powers[^10];

slide-25
SLIDE 25

# Sequences my @powers = 2, 4, 8 ... *; say @powers[^10];

slide-26
SLIDE 26

# Sequences my @fibonacci = 0, 1, 1 ... *; say @fibonacci[^10];

slide-27
SLIDE 27

# Sequences my @fibonacci = 0, 1, {$^x + $^y} ... *; say @fibonacci[^10];

slide-28
SLIDE 28

# Metaoperators my @a = ( 1, 2, 3); my @b = (10, 20, 30); say @a <<+>> @b; say 2 <<*<< @a; say [lcm] @a;

slide-29
SLIDE 29

# Hyperoperators my @a = (1,2,3); my @b = <a b c>; say @a X @b; say @a Z @b;

slide-30
SLIDE 30

# Hyperoperators my @a = (1,2,3); my @b = <a b c>; say @a X~ @b; say @a Z~ @b;

slide-31
SLIDE 31

# Object Oriented class Dog { }

slide-32
SLIDE 32

# Object Oriented class Dog { has Str $.name is required; has Int $.age; method bark(Str $target) { say "$!name barked at $target"; } }

slide-33
SLIDE 33

# Object Oriented class Dog { has Str $.name is required; has Int $.age; method bark(Str $target) { say "$!name barked at $target"; } } my Dog $fido = Dog.new(name => "Fido"); $fido.bark("the audience");

slide-34
SLIDE 34

# Functional sub add($a, $b) { return $a + $b } sub make_add($b) { return sub ($a) { add($a, $b) } } my &add_3 = make_add(3); say add_3(2);

slide-35
SLIDE 35

# Functional my &add_3 = * + 3; say add_3(2);

slide-36
SLIDE 36

# Multi-dispatch multi sub sort(@list where *.elems < 2) { return @list; } multi sub sort(@list where *.elems >= 2) { my $pivot = @list[0]; my @before = @list[1 .. *].grep(* before $pivot); my @after = @list[1 .. *].grep(* !before $pivot); return flat sort(@before), $pivot, sort(@after); } say sort (14, 1, 61, 25, 8);

slide-37
SLIDE 37

# Regular Expressions my $string = "length = 5"; $string ~~ /length \s* "=" \s* (\d+)/; say $0.Int;

slide-38
SLIDE 38

# Regular Expressions my $string = "length = 5"; $string ~~ /length \s* "=" \s* $<length> = (\d+) /; say $<length>;

slide-39
SLIDE 39

# Regular Expressions grammar Params { rule TOP { <assignment>+ } rule assignment { <param> "=" <value> ";" } token param { <.alpha>+ } token value { <.digit>+ } } my $string = "length = 5; width = 20; time = 4;"; my $parsed = Params.parse($string); say $parsed;

slide-40
SLIDE 40

# Concurrency my $promise = start { my $x = 0; for 1 ... 10 -> $i { $x += $i } $x; } my $result = await $promise; say $result;

slide-41
SLIDE 41

# Concurrency my $veg_supplier = Supplier.new; my $feedback = supply { whenever $veg_supplier.Supply { emit("We've got veg: " ~ $_); }; } $feedback.tap( -> $str { say "$str" }); $veg_supplier.emit("Radish"); $veg_supplier.emit("Lettuce"); $veg_supplier.emit("Tomato");

slide-42
SLIDE 42

Useful Links The Perl 6 documentation https://docs.perl6.org Rakudo, a Perl 6 implementation https://rakudo.org Online REPL https://glot.io/new/perl6 Rosetta Code https://rosettacode.org