127
$@%!»; 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. [email protected]

$@%!»; – Perl 6

Embed Size (px)

DESCRIPTION

$@%!»; – Perl 6. Frank Blendinger. [email protected]. 2013-10-21. - PowerPoint PPT Presentation

Citation preview

Page 1: $@%!»;  –  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.

[email protected]

Page 2: $@%!»;  –  Perl 6

204/20/23 / Method Park Software AG

Disclaimer

Page 3: $@%!»;  –  Perl 6

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

Page 4: $@%!»;  –  Perl 6

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”

Page 5: $@%!»;  –  Perl 6

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::*)

Page 6: $@%!»;  –  Perl 6

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

Page 7: $@%!»;  –  Perl 6

704/20/23 / Method Park Software AG

Perl 6 Language Specification

http://perlcabal.org/syn/

Page 8: $@%!»;  –  Perl 6

804/20/23 / Method Park Software AG

Perl 6 Language Specification

https://github.com/perl6/specs

Page 9: $@%!»;  –  Perl 6

904/20/23 / Method Park Software AG

Perl 6 Language Specification

Page 10: $@%!»;  –  Perl 6

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”

Page 11: $@%!»;  –  Perl 6

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

Page 12: $@%!»;  –  Perl 6

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

Page 13: $@%!»;  –  Perl 6

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

Page 14: $@%!»;  –  Perl 6

1404/20/23 / Method Park Software AG

Perl 5

#!/usr/bin/env perl use strict; use warnings;

Page 15: $@%!»;  –  Perl 6

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!

Page 16: $@%!»;  –  Perl 6

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

Page 17: $@%!»;  –  Perl 6

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

Page 18: $@%!»;  –  Perl 6

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

Page 19: $@%!»;  –  Perl 6

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

Page 20: $@%!»;  –  Perl 6

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'?

Page 21: $@%!»;  –  Perl 6

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

Page 22: $@%!»;  –  Perl 6

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'};

Page 23: $@%!»;  –  Perl 6

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

Page 24: $@%!»;  –  Perl 6

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

Page 25: $@%!»;  –  Perl 6

2504/20/23 / Method Park Software AG

Perl 6 Basics

Page 26: $@%!»;  –  Perl 6

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

Page 27: $@%!»;  –  Perl 6

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

Page 28: $@%!»;  –  Perl 6

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! }

Page 29: $@%!»;  –  Perl 6

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; }

Page 30: $@%!»;  –  Perl 6

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

Page 31: $@%!»;  –  Perl 6

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

Page 32: $@%!»;  –  Perl 6

3204/20/23 / Method Park Software AG

Dealing with Arrays

sub shout-words(@words) { for @words -> $word { say uc($word); } }

Page 33: $@%!»;  –  Perl 6

3304/20/23 / Method Park Software AG

Dealing with Arrays

sub shout-words(@words) { for @words { say uc($_); # topic variable

} }

Page 34: $@%!»;  –  Perl 6

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

Page 35: $@%!»;  –  Perl 6

3504/20/23 / Method Park Software AG

Dealing with Hashes

sub say-names-with-age(%people) {

for %people.kv -> $key, $value {

say $key ~ " (" ~ $value ~ ")";

}

}

Page 36: $@%!»;  –  Perl 6

3604/20/23 / Method Park Software AG

Dealing with Hashes

sub say-names-with-age(%people) {

for %people {

say $_.key ~ " (" ~ $_.value ~ ")";

}}

Page 37: $@%!»;  –  Perl 6

3704/20/23 / Method Park Software AG

Dealing with Hashes

sub say-names-with-age(%people) {

for %people {

say .key ~ " (" ~ .value ~ ")";

}

}

Page 38: $@%!»;  –  Perl 6

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

Page 39: $@%!»;  –  Perl 6

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

Page 40: $@%!»;  –  Perl 6

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

Page 41: $@%!»;  –  Perl 6

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

Page 42: $@%!»;  –  Perl 6

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

Page 43: $@%!»;  –  Perl 6

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!

Page 44: $@%!»;  –  Perl 6

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 …

Page 45: $@%!»;  –  Perl 6

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

Page 46: $@%!»;  –  Perl 6

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;

Page 47: $@%!»;  –  Perl 6

4704/20/23 / Method Park Software AG

Varargs with Slurpy Arrays

# default (no "()" given!) is *@_, so:

sub sum {

[+] @_;

}

say sum 42, 23, 1337;

Page 48: $@%!»;  –  Perl 6

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!)

Page 49: $@%!»;  –  Perl 6

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

Page 50: $@%!»;  –  Perl 6

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

Page 51: $@%!»;  –  Perl 6

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

Page 52: $@%!»;  –  Perl 6

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

Page 53: $@%!»;  –  Perl 6

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

Page 54: $@%!»;  –  Perl 6

5404/20/23 / Method Park Software AG

Introspection

What are you?

42.WHAT; # (Int)(1/2).WHAT; # (Rat)

Page 55: $@%!»;  –  Perl 6

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 …

Page 56: $@%!»;  –  Perl 6

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

Page 57: $@%!»;  –  Perl 6

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

Page 58: $@%!»;  –  Perl 6

5804/20/23 / Method Park Software AG

We Have to Go Deeper!

Page 59: $@%!»;  –  Perl 6

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;

Page 60: $@%!»;  –  Perl 6

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

Page 61: $@%!»;  –  Perl 6

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

Page 62: $@%!»;  –  Perl 6

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])

Page 63: $@%!»;  –  Perl 6

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'

Page 64: $@%!»;  –  Perl 6

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'

Page 65: $@%!»;  –  Perl 6

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

Page 66: $@%!»;  –  Perl 6

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

Page 67: $@%!»;  –  Perl 6

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

##########

##

##

####

####

####

####

Page 68: $@%!»;  –  Perl 6

6804/20/23 / Method Park Software AG

Object Oriented Programming

Page 69: $@%!»;  –  Perl 6

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;

Page 70: $@%!»;  –  Perl 6

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

Page 71: $@%!»;  –  Perl 6

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]

Page 72: $@%!»;  –  Perl 6

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]

Page 73: $@%!»;  –  Perl 6

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

Page 74: $@%!»;  –  Perl 6

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

Page 75: $@%!»;  –  Perl 6

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]

Page 76: $@%!»;  –  Perl 6

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]

Page 77: $@%!»;  –  Perl 6

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;

Page 78: $@%!»;  –  Perl 6

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)

Page 79: $@%!»;  –  Perl 6

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;

Page 80: $@%!»;  –  Perl 6

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;

Page 81: $@%!»;  –  Perl 6

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;

Page 82: $@%!»;  –  Perl 6

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;

Page 83: $@%!»;  –  Perl 6

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

Page 84: $@%!»;  –  Perl 6

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 } }

Page 85: $@%!»;  –  Perl 6

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; # :-)

Page 86: $@%!»;  –  Perl 6

8604/20/23 / Method Park Software AG

Stubs

role AbstractSerializable { method serialize() { ... } }

Page 87: $@%!»;  –  Perl 6

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…

Page 88: $@%!»;  –  Perl 6

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

Page 89: $@%!»;  –  Perl 6

8904/20/23 / Method Park Software AG

Functional Programming

Page 90: $@%!»;  –  Perl 6

9004/20/23 / Method Park Software AG

Closures

my @times = (); # Array.new

for 1..10 {

my $multiplicant = $_;

@times[$_] = sub ($a) { $a * $multiplicant };

}

Page 91: $@%!»;  –  Perl 6

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

Page 92: $@%!»;  –  Perl 6

9204/20/23 / Method Park Software AG

Infinite Lists, Lazy Evaluation

my @all-the-integers = 0..Inf; for ^10 { say @all-the-integers[$_] }

Page 93: $@%!»;  –  Perl 6

9304/20/23 / Method Park Software AG

Infinite Lists, Lazy Evaluation

my @all-the-integers = 0..Inf; for ^10 { say @all-the-integers[$_] }

Page 94: $@%!»;  –  Perl 6

9404/20/23 / Method Park Software AG

map

Page 95: $@%!»;  –  Perl 6

9504/20/23 / Method Park Software AG

map

(1..10).map( { $_ * $_ } ).join(", ").say;

Page 96: $@%!»;  –  Perl 6

9604/20/23 / Method Park Software AG

map

(1..10).map( { $_ * $_ } ).join(", ").say;

# 1, 4, 9, 16, 25, 36, 49, 64, 81, 100

Page 97: $@%!»;  –  Perl 6

9704/20/23 / Method Park Software AG

Z – the zip operator

Page 98: $@%!»;  –  Perl 6

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

Page 99: $@%!»;  –  Perl 6

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

Page 100: $@%!»;  –  Perl 6

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; }

Page 101: $@%!»;  –  Perl 6

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

Page 102: $@%!»;  –  Perl 6

10204/20/23 / Method Park Software AG

Meta Operators

Page 103: $@%!»;  –  Perl 6

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)

Page 104: $@%!»;  –  Perl 6

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;

Page 105: $@%!»;  –  Perl 6

10504/20/23 / Method Park Software AG

The Hyper Meta Operators: » / «

Page 106: $@%!»;  –  Perl 6

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;

Page 107: $@%!»;  –  Perl 6

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;

Page 108: $@%!»;  –  Perl 6

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.

Page 109: $@%!»;  –  Perl 6

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;

Page 110: $@%!»;  –  Perl 6

11004/20/23 / Method Park Software AG

Multi

Page 111: $@%!»;  –  Perl 6

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}."}

Page 112: $@%!»;  –  Perl 6

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.

Page 113: $@%!»;  –  Perl 6

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

Page 114: $@%!»;  –  Perl 6

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>

Page 115: $@%!»;  –  Perl 6

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

Page 116: $@%!»;  –  Perl 6

11604/20/23 / Method Park Software AG

given/when

Page 117: $@%!»;  –  Perl 6

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 } }

Page 118: $@%!»;  –  Perl 6

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" } }

Page 119: $@%!»;  –  Perl 6

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" } }

Page 120: $@%!»;  –  Perl 6

12004/20/23 / Method Park Software AG

Smart Matching with ~~

Page 121: $@%!»;  –  Perl 6

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

Page 122: $@%!»;  –  Perl 6

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;

Page 123: $@%!»;  –  Perl 6

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/;

Page 124: $@%!»;  –  Perl 6

12404/20/23 / Method Park Software AG

One More Thing

Awesome new regex syntax

… and beyond: rules, tokens, grammars

It’s a parsing wonderland!

Page 125: $@%!»;  –  Perl 6

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. :-)

Page 126: $@%!»;  –  Perl 6

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/

Page 127: $@%!»;  –  Perl 6

12704/20/23 / Method Park Software AG

Questions?