45
Introducing Perl 6 Nuno Carvalho <[email protected]> May 22, 2010 Introducing Perl 6 Portuguese Perl Workshop 2010

Introducing perl6

Embed Size (px)

Citation preview

Page 1: Introducing perl6

Introducing Perl 6

Nuno Carvalho <[email protected]>

May 22, 2010

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 2: Introducing perl6

Introduction

• Perl 6 is a specification

• Several implementations• Anything that passes the spec test• Perl 6 is not a replacement for Perl 5

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 3: Introducing perl6

Introduction

• Perl 6 is a specification• Several implementations

• Anything that passes the spec test• Perl 6 is not a replacement for Perl 5

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 4: Introducing perl6

Introduction

• Perl 6 is a specification• Several implementations• Anything that passes the spec test

• Perl 6 is not a replacement for Perl 5

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 5: Introducing perl6

Introduction

• Perl 6 is a specification• Several implementations• Anything that passes the spec test• Perl 6 is not a replacement for Perl 5

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 6: Introducing perl6

Perl6::Variables• scalar values

1 $scalar

• arrays

1 @array2 @array[$i]

• hash tables

1 %hash2 %hash{$key}

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 7: Introducing perl6

Perl6::Variables• scalar values

1 $scalar

• arrays

1 @array2 @array[$i]

• hash tables

1 %hash2 %hash{$key}

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 8: Introducing perl6

Perl6::Variables• scalar values

1 $scalar

• arrays

1 @array2 @array[$i]

• hash tables

1 %hash2 %hash{$key}

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 9: Introducing perl6

Perl6::Operators

• the assignment operator (=)

1 $string = ’Perl’;

• the dot operator (.)

1 $object.method;2 $file.lines;3 @array.sort;

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 10: Introducing perl6

Perl6::Operators

• the assignment operator (=)

1 $string = ’Perl’;

• the dot operator (.)

1 $object.method;2 $file.lines;3 @array.sort;

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 11: Introducing perl6

Perl6::Operators

• the list operator (,)

1 @array = 1, 2, 3, 4;

• the fat-arrow operator (=>)

1 $pair = ’color’ => ’black’;2 $pair.key; # color3 $pair.value; # black

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 12: Introducing perl6

Perl6::Operators• string concatenation (˜)

1 ’Pe’ ˜ ’rl’; # Perl

• the max operator (max)

1 1 max 2; # returns 22 1 max 2 max 3; # returns 3

• the reduction meta operator ([])

1 [max] 1, 2; # returns 22 [+] 1, 2; # returns 3

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 13: Introducing perl6

Perl6::Operators• string concatenation (˜)

1 ’Pe’ ˜ ’rl’; # Perl

• the max operator (max)

1 1 max 2; # returns 22 1 max 2 max 3; # returns 3

• the reduction meta operator ([])

1 [max] 1, 2; # returns 22 [+] 1, 2; # returns 3

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 14: Introducing perl6

Perl6::Operators• string concatenation (˜)

1 ’Pe’ ˜ ’rl’; # Perl

• the max operator (max)

1 1 max 2; # returns 22 1 max 2 max 3; # returns 3

• the reduction meta operator ([])

1 [max] 1, 2; # returns 22 [+] 1, 2; # returns 3

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 15: Introducing perl6

Perl6::Operators

• the repetition operator (x)

1 ’a’ x 3; # ’aaa’

• the ternary operator (?? !!)

1 $n == 0 ?? ’zero’ !! ’non-zero’;

• another list constructor (<>)

1 @week = <Mon Tue Wed Thu Fri>;

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 16: Introducing perl6

Perl6::Operators

• the repetition operator (x)

1 ’a’ x 3; # ’aaa’

• the ternary operator (?? !!)

1 $n == 0 ?? ’zero’ !! ’non-zero’;

• another list constructor (<>)

1 @week = <Mon Tue Wed Thu Fri>;

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 17: Introducing perl6

Perl6::Operators

• the repetition operator (x)

1 ’a’ x 3; # ’aaa’

• the ternary operator (?? !!)

1 $n == 0 ?? ’zero’ !! ’non-zero’;

• another list constructor (<>)

1 @week = <Mon Tue Wed Thu Fri>;

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 18: Introducing perl6

Perl6::Operators

• the yadayada operator (...)

1 sub do_something() { ... }

• the ternary operator (?? !!)

1 $n == 0 ?? ’zero’ !! ’non-zero’;

• the arrow operator (->)

1 for @week -> @day { ... }

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 19: Introducing perl6

Perl6::Operators

• the yadayada operator (...)

1 sub do_something() { ... }

• the ternary operator (?? !!)

1 $n == 0 ?? ’zero’ !! ’non-zero’;

• the arrow operator (->)

1 for @week -> @day { ... }

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 20: Introducing perl6

Perl6::Operators

• the yadayada operator (...)

1 sub do_something() { ... }

• the ternary operator (?? !!)

1 $n == 0 ?? ’zero’ !! ’non-zero’;

• the arrow operator (->)

1 for @week -> @day { ... }

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 21: Introducing perl6

Perl6::Subroutines

• typical subroutine

1 sub hello() {2 say ’hello world’;3 }4

5 hello(); # says hello world

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 22: Introducing perl6

Perl6::Subroutines

• anonymous subroutine

1 my $hello = sub {2 say ’hello world’;3 }4

5 $hello.(); # says hello world

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 23: Introducing perl6

Perl6::Subroutines

• passing values

1 sub hello($name) {2 say "hello $name";3 }4

5 hello(’p6’); # says hello p6

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 24: Introducing perl6

Perl6::Subroutines

• passing a read-write reference

1 sub inc($counter is rw) {2 $counter++;3 }4

5 my $c = 0;6 inc($c); # $c is 17 inc($c); # $c is 2

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 25: Introducing perl6

Perl6::Subroutines

• passing a copy of value

1 sub inc($counter is copy) {2 $counter++;3 }4

5 my $c = 0;6 inc($c); # $c is 07 inc($c); # $c is 0

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 26: Introducing perl6

Perl6::Subroutines

• slurpy arguments

1 sub inc(*$counter is copy) {2 XXX3 }4

5 XXX

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 27: Introducing perl6

Perl6::Subroutines

• passing a list

1 sub check(@words) { ... }

• passing an hash

1 sub check(%hash) { ... }

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 28: Introducing perl6

Perl6::Subroutines

• passing a list

1 sub check(@words) { ... }

• passing an hash

1 sub check(%hash) { ... }

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 29: Introducing perl6

Perl6::Multi

1 multi add(Real $x, Real $y) { ... }2 multi add(Int $x, Int $y) { ... }3 multi add($x, $y) { ... }4 multi add($x where $x>0, $y) { ... }

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 30: Introducing perl6

Perl6::Objects

1 class Point {2 has $!x;3 has $!y;4 has $.color;5

6 method new($x, $y, $color) { ... }7 method distance($x, $y) { ... }8 }

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 31: Introducing perl6

Perl6::Roles

1 XXX

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 32: Introducing perl6

Perl6::Regexes

1 XXX

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 33: Introducing perl6

Perl6::Regexes

1 XXX

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 34: Introducing perl6

Perl6::Grammars

1 XXX

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 35: Introducing perl6

Rakudo

• one Perl 6 implementation• passes 79% of the test spec• runs on Parrot VM• http://www.rakudo.org

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 36: Introducing perl6

Get your feet wet

• install rakudo

1 $ git clone ...2 $ perl Configure.pl --gen-parrot3 $ make4 $ make test5 $ make spectest

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 37: Introducing perl6

Run Perl 6 code

• interactive mode

1 rakudo$ ./perl62 > say ’hello world’;3 hello world

• execute scripts

1 rakudo$ cat hello.p62 say ’hello world’;3 rakudo$ ./perl6 hello.p64 hello world

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 38: Introducing perl6

Run Perl 6 code

• interactive mode

1 rakudo$ ./perl62 > say ’hello world’;3 hello world

• execute scripts

1 rakudo$ cat hello.p62 say ’hello world’;3 rakudo$ ./perl6 hello.p64 hello world

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 39: Introducing perl6

Thanks

• Perl 6• Parrot• Rakudo• Perl 6 Book

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 40: Introducing perl6

Conlusion

• we haven’t seen everything

• spec is still under development• implementations still under development• http://www.perl6.org• .. available in a Christmas near you

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 41: Introducing perl6

Conlusion

• we haven’t seen everything• spec is still under development

• implementations still under development• http://www.perl6.org• .. available in a Christmas near you

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 42: Introducing perl6

Conlusion

• we haven’t seen everything• spec is still under development• implementations still under development

• http://www.perl6.org• .. available in a Christmas near you

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 43: Introducing perl6

Conlusion

• we haven’t seen everything• spec is still under development• implementations still under development• http://www.perl6.org

• .. available in a Christmas near you

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 44: Introducing perl6

Conlusion

• we haven’t seen everything• spec is still under development• implementations still under development• http://www.perl6.org• .. available in a Christmas near you

Introducing Perl 6 Portuguese Perl Workshop 2010

Page 45: Introducing perl6

The End

Questions?

Introducing Perl 6 Portuguese Perl Workshop 2010