1.27k likes | 1.36k Views
$@%!»; – Perl 6. Frank Blendinger. fbr@methodpark.de. 2013-10-21.
E N D
$@%!»; – Perl 6 Frank Blendinger • fbr@methodpark.de • 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.
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 3
Perl Philosophy • TIMTOWTDO: There Is More Than One Way To Do It • DWIM: Do What I Mean • Keep easy things easy and hard things possibleslurp("filename").lines • Huffman coding • Common things → short name • Crazy stuff → long names • “Weird things should look weird” 4
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::*) 5
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 6
Perl 6 Language Specification • http://perlcabal.org/syn/ 7
Perl 6 Language Specification • https://github.com/perl6/specs 8
Perl 6 Implementations • »Perl 6 is anything that passes • the official test suite.« • Larry Wall, “Synopsis 1: Overview” 10
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 11
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 12
Remove Inconsistencies • »In Perl 6, we decided it would be better to fix the language than fix the user.« • Larry Wall 13
Perl 5 • #!/usr/bin/env perl • use strict; • use warnings; • … 14
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! 15
Sigil Invariance • Perl 5 • my $scalar = "sclr"; • my @array = (1, 2, 3); • my %hash = (foo => 23, bar => 42); 16
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 17
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"; 18
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"; 19
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'? 20
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"; 21
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'}; 22
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>; 23
Sigil Invariance • # Perl 5: get list from hash of hashes • my @verbs = @{ $dict { 'verb' }{ 'transitive' } }; # Perl 6: get list from hash of hashes my @verbs = %dict{ 'verb' }{ 'transitive' }; # Perl 6: or even like this my @verbs = %dict<verb><transitive>; 24
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 26
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 27
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! • } 28
Subroutines • sub stutterify($word is rw) { • my $first-letter = substr($word, 0, 1); • my $wwwword = ($first-letter ~ "-") x 3 ~ $word; • $word = $wwwword; • } 29
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 30
Subroutines • # pass-by-value • sub 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"; 31
Dealing with Arrays • sub shout-words(@words) { • for @words -> $word { • say uc($word); • } • } 32
Dealing with Arrays • sub shout-words(@words) { • for @words { • say uc($_); # topic variable • } • } 33
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 34
Dealing with Hashes • sub say-names-with-age(%people) { • for %people.kv -> $key, $value { • say $key ~ " (" ~ $value ~ ")"; • } • } 35
Dealing with Hashes • sub say-names-with-age(%people) { • for %people { • say $_.key ~ " (" ~ $_.value ~ ")"; • } • } 36
Dealing with Hashes • sub say-names-with-age(%people) { • for %people { • say .key ~ " (" ~ .value ~ ")"; • } • } 37
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); 38
Optional Parameters • sub whatever($optional?) { • if defined($optional) { • say "$optional is pretty cool."; • } else { • say "Whatever, man."; • } • } • whatever("The dude"); • whatever(); 39
Named Parameters • sub protip(:$what, :$who) { • say "Use $what, $who!"; • } • protip(:what("the force"), :who("Luke")); • protip(:who("Luke"), :what("the force")); 40
Named Parameters • sub protip(:$what, :$who) { • say "Use $what, $who!"; • } • # short form for strings • protip(:who<Luke>, :what<the force>); 41
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)); 42
Named Parameters • sub protip(:$what, :$who) { • say "Use $what, $who!"; • } • my $what = "the force"; • my $who = "Luke"; • protip(:$who, :$what); # names match → magic! 43
Required Named Parameters • sub protip(:$what, :$who) { • say "Use $what, $who!"; • } • # named params are optional by default • protip(); • # use of uninitialized value of type … 44
Required Named Parameters • sub protip(:$what!, :$who!) { • say "Use $what, $who!"; • } • # named params are optional by default • protip(); • # Required named parameter 'what' not passed 45
Varargs with Slurpy Arrays • # varargs with slurpy arrays (* prefix) • sub sum(*@nums) { • [+] @nums; • } • say sum 42, 23, 1337; 46
Varargs with Slurpy Arrays • # default (no "()" given!) is *@_, so: • sub sum { • [+] @_; • } • say sum 42, 23, 1337; 47
Procedural vs. Object Oriented • Perl 6 is object-oriented at its core • You can use it, but you don’t have to (TIMTOWTDI!) 48
Procedural vs. Object Oriented • Perl 6 is object-oriented at its core • You can use it, but you don’t have to (TIMTOWTDI!) • # procedural • say split(" ", capitalize("hello world"))[1]; • # World 49
Procedural vs. Object Oriented • Perl 6 is object-oriented at its core • You can use it, but you don’t have to (TIMTOWTDI!) • # procedural • say split(" ", capitalize("hello world"))[1]; • # World • # object oriented • "hello world".capitalize.split(" ")[1].say; • # World 50