1 / 80

Perl6

Perl6. Twenty Things I Love, Five Things You’ll Hate. Eric Maki – eric@uc.org Oct. 21/2004. Slide Preface. These slides originally accompanied a presentation I did for the KW PerlMongers (http://kw.pm.org) on Oct. 21/2004.

cybele
Download Presentation

Perl6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.org Oct. 21/2004

  2. Slide Preface These slides originally accompanied a presentation I did for the KW PerlMongers (http://kw.pm.org) on Oct. 21/2004. The slides borrow some materials from the Synopses (see http:// dev.perl.org/perl6) Some corrections and additions have been made post-presentation. Please send any questions or corrections to eric@uc.org. • - Eric Maki Perl6: Twenty Things I Love,Five Things You’ll Hate

  3. Introduction Perl6 is a massive topic: • Damian Conway regularly gives 5 hour talks where he lightly covers 60% of the key topics. • Larry Wall has produced a series of Apocalypses that now print to about 240 pages. He still hasn’t covered everything. This presentation is < 1 hour. I won’t be getting to everything. Perl6: Twenty Things I Love,Five Things You’ll Hate

  4. Introduction This talk will cover 20ish of the Perl6 features that I particularly like, and five things that you might not like. The five negatives are first… then the good news. Each of these 25 topics gets about 2 minutes. Please save your questions for the end. Perl6: Twenty Things I Love,Five Things You’ll Hate

  5. Five Things You’ll Hate • No backwards compatibility. • Much more complex language. • Non ASCII opérators; white-space now (occasionally) matters. • End to "enforced" open source. • Ready to ship in { (localtime)[5] + 1902; }. Perl6: Twenty Things I Love,Five Things You’ll Hate

  6. 1. Backwards Compatibility • Unlike the Perl4  Perl5 transition, Perl5  Perl6 does not strive to be maximally backwards compatible. • Virtually all non-trivial Perl5 code will break if you try to compile it as Perl6. • If you have Perl5 code, you have broken Perl6 code. Perl6: Twenty Things I Love,Five Things You’ll Hate

  7. 1. Backwards Compatibility Things that will break in most programs: • sigils have different semantics • Loop reform: foreach no longer exists, three part for loops break • trinary $x?1:0 breaks • bitwise & and | break • virtually all OO breaks: -> renamed • string concat (.) breaks Perl6: Twenty Things I Love,Five Things You’ll Hate

  8. 1. Backwards Compatibility Why? # Perl 5: my $name = "Eric"; print "$name's dog is missing.\n"; This prints " dog is missing." because the single quote was the (very) old package delimiter. ie: perl interprets this as $name::s Maintaining backwards compatibility leaves a mess. Perl6: Twenty Things I Love,Five Things You’ll Hate

  9. 1. Backwards Compatibility Mitigation Fortunately, we don’t need to care too much: • Perl5 code can compile to parrot bytecode thanks to Ponie. • Perl6 designed so that the interpreter knows when it has Perl5 vs. Perl6, and will Do The Right Thing. • For example, all of CPAN will run on Parrot*, and interoperate with Perl6 programs. Perl6: Twenty Things I Love,Five Things You’ll Hate

  10. 2. Complexity Here are some of the new keywords/core functions in Perl6: given, when, is, loop, try, module, class, method, multi, submethod, zip, take, gather, macro, rule, grammar, let, say, state, rw, hash, of, returns, but, leave, temp, assuming, default, does, any, one, all, none, true, err, fail, rx There will be a test at the end. Perl6: Twenty Things I Love,Five Things You’ll Hate

  11. 2. Complexity There is a lot more depth to Perl6. I’ve probably spent: • 40+ hours reading about Perl6 • 7+ hours listening to Damian talk about Perl6 I’m pretty sure I am just scratching the surface. I expect Programming Perl, 4th edition to be in the region of 4000 pages. (From its currently slender 1008 pages.) Perl6: Twenty Things I Love,Five Things You’ll Hate

  12. 2. Complexity Mitigation With this complexity comes a dazzling amount of power. Nothing is added to satisfy edge cases. Additions are broad and flexible ideas that reduce complexity. In fact, there are less odd exceptions and strange and unpredictable corners (at least so far) in Perl 6. Perl6: Twenty Things I Love,Five Things You’ll Hate

  13. 2. Complexity Mitigation 2 Perl6 shouldn’t be much harder to get started with than Perl5: # perl 5 print "Hello World.\n"; # perl 6 – same as above, or: say "Hello World."; Perl6: Twenty Things I Love,Five Things You’ll Hate

  14. 3. Syntax Beefs Non ASCII Opérators, Whitespace Changes This might sound like two points, but it isn’t. These are the syntactic pet-peeves of those of the perlish persuasion: • Whitespace should never matter… that’s downright… Python. • I’m a North American. I don’t even know how to type a ¥ or «these things». Perl6: Twenty Things I Love,Five Things You’ll Hate

  15. 3.0 Non ASCII Opérators ¥ and «these» are operators in Perl6: « and » The “French quotes” « and » play an important role. They are now a synonym for qw{}, identify hyperoperators, and are a synonym for %x{'this'}ie: %x«this» ¥ The yen symbol ¥ is the new infix version of the zip operator. Perl6: Twenty Things I Love,Five Things You’ll Hate

  16. 3.0 Non ASCII Opérators People seem to hate this. But you aren’t forced to use either. You can use the <<Freedom Quotes>> anywhere you want «French Quotes». And you can always just use zip()instead of the infix operator ¥. Perl6: Twenty Things I Love,Five Things You’ll Hate

  17. 3.5 Whitespace Matters There are a handful of places where whitespace now matters. The big one is that there cannot be a space between a hash and its subscript: %monsters{'cookie'} = Monster.new; # Valid %people {'john'} = Person.new; # Not Valid In the second example, the interpreter sees {'john'} as a closure, which causes a syntax error. Perl6: Twenty Things I Love,Five Things You’ll Hate

  18. 3.5 Whitespace Matters Mitigation: If you want to line up your subscripts, or have any other reason for putting a space there, you can. %monsters.{'cookie'} = Monster.new; %people .{'john'} = Person.new; I'm not sure that I have ever seen anyone put a space between a hash and a subscript... but many will hate this for ideological reasons. Perl6: Twenty Things I Love,Five Things You’ll Hate

  19. 4. End To Enforced Open Source In current Perl culture, there is a distinction between “Open Source” and “Available Source”... But there is no “Closed Source”. With the new Parrot backend, perl (or C, or Python) code can be compiled and shipped as bytecode. Good thing? Bad thing? Probably both. Perl6: Twenty Things I Love,Five Things You’ll Hate

  20. 5. Ships in {(localtime)[5] + 1902;} Perl6 has been 2 years off for quite some time. My current guess is: • Perl 6 Alpha – 2 years • Perl 6 Beta – 3 years • Perl 6 stable enough for production environment – 4 years. • Convince your boss to migrate – 8 years. • Perl6 code outweighs Perl5 code – 55 years. Perl6: Twenty Things I Love,Five Things You’ll Hate

  21. 5. Ships in {(localtime)[5] + 1902;} Next year’s guess: • Perl 6 Alpha – 2 years. • Perl 6 Beta – 3 years. • Perl 6 stable enough for production environment – 4 years. • Convince your boss to migrate – 8 years. • Perl6 code outweighs Perl5 code – 55 years. Perl6: Twenty Things I Love,Five Things You’ll Hate

  22. 5. Ships in {(localtime)[5] + 1902;} But Perl 6 isn't pure vapourware: • Parrot pre-Alpha (0.1.0) can be downloaded right now. • The compiler is not complete, but it does compile a subset of Perl6. • There are also compilers for two mini-languages: Jako and Cola, as well as Parrot Assembly. • Compilers for Ruby, Scheme, BASIC, PHP, Python and Forth are all in various stages of completion. Perl6: Twenty Things I Love,Five Things You’ll Hate

  23. 5 Things You’ll Hate That was the bad news • It’s new. • It’s different. • It’s scary. • Why don’t I have it yet? Now for the exciting stuff. But first… Perl6: Twenty Things I Love,Five Things You’ll Hate

  24. 20 Things I Love my $time_remaining = KWPM::TalkClock::time_remain( time() ); if ( $time_remaining > 0.80 ) { $presenter->take_questions( 2 ); } elsif ( $time_remaining == 0.80 ) { $presenter->pat( -location => ’back’, -duration => 10 ); } else { $presenter->hurry_up( -factor => 2, -apologize => 1 ); } Perl6: Twenty Things I Love,Five Things You’ll Hate

  25. 20 Things I Love Perl 6 Version given KWPM::TalkClock::time_remain( time() ) { when { $_ > 0.80 } { $presenter.take_questions( 2 ) } when 0.80 { $presenter.pat( :location«back» :duration(10) ) } default { $presenter.hurry_up( :factor(2) :apologize ) } } Perl6: Twenty Things I Love,Five Things You’ll Hate

  26. 1. Parrot Architecture True separation of compiler and interpreter. • Faster. • Recompile only when needed. • Interoperate with other languages (Python, C, C++, Java, Perl5, Perl1). • No more XS. • For more, see Lloyd. Perl6: Twenty Things I Love,Five Things You’ll Hate

  27. 2. Types, If You Want Them Perl is no longer typeless. It’s now type-optional. Built-in object types start with an uppercase letter: Int, Num, Str, Bit, Ref, Scalar, Array, Hash, Rule and Code. Value types are lowercase: int, num, str, bit, and ref Perl6: Twenty Things I Love,Five Things You’ll Hate

  28. 2. Types, If You Want Them Value types are “unboxed”. They let you have “slender” arrays: my Array of int @array; # or equivalently… my int @array; You can still treat an int like an Int, but without cached stringification, etc. (called autoboxing) Among many other benefits this allows true subroutine signatures and fine-grained overloading. More on both later. Perl6: Twenty Things I Love,Five Things You’ll Hate

  29. 2. Types, If You Want Them Examples of Declarations: (from s06) my Hash of Array of Recipe %book; my %book of Hash of Array of Recipe; # same thing # sub declaration - note lexical sub! my sub get_book returns Hash of Array of Recipe {...} # a junctive type, more on that later my Int|Str $error = $val; Perl6: Twenty Things I Love,Five Things You’ll Hate

  30. 3. Everything is an Object, Sort Of Everything can act like an object: “Perl 6 is an OO engine, but you're not generally required to think in OO when that's inconvenient. However, some built-in concepts such as filehandles will be more object-oriented in a user-visible way.” -- from s06 Example: Hash iterators for %myhash.keys -> $key {…} while %myhash.values { … } for %myhash.kv -> $key, $val { … } Perl6: Twenty Things I Love,Five Things You’ll Hate

  31. 3. Everything is an Object, Sort Of Example: Strings No more scalar() vs. length() confusion: $foo.chars $foo.bytes @foo.elems @foo.length There will likely be things like @array.pop() and so forth. You won’t have to use them if you don’t want to. Filehandles and so forth will have lots of OO goodness. But Larry hasn’t gotten that far yet. Perl6: Twenty Things I Love,Five Things You’ll Hate

  32. 3. Everything is an Object, Sort Of A Few More Examples: $serialized = %deep_hash.perl; # like Dumper() say $some_float.as( ‘%03d’ ); # implicit sprintf for ( 0..@foo.end ) { … } # replaces @#foo Perl6: Twenty Things I Love,Five Things You’ll Hate

  33. 4. Different Things are More Different In Perl 5: • An object is just a blessed reference. • A class was just a package. • A method was just a sub. • A module was just a file while defined a package with the same name. • Overloading, inheritance, constants and a host of other things were squished into the "use" keyword. Perl6: Twenty Things I Love,Five Things You’ll Hate

  34. 4. Different Things are More Different Different things should look different. That way, you know they are different. A lot of Perl5 OO gets methods and subs confused, because they look the same. Keywords are cheap. Ambiguity is expensive. Keeping a language simple by minimizing concepts doesn’t work. Perl6: Twenty Things I Love,Five Things You’ll Hate

  35. 4. Different Things are More Different In Perl 6 Different things are different: • A module is a module. • A class is a class. • An object is an object. • A method is a method. • Overloading is sensible. • Constants are variables with properties, not pragma. Similarly, same things are a bit more same. Perl6: Twenty Things I Love,Five Things You’ll Hate

  36. 5. Sigils: Simplified In Perl6, sigils don’t vary when you refer to parts of them rather than the whole: my %pets; %pets = ( ‘eric’ => ‘rat’, ‘jimmy’ => ‘dog’, … ); say %pets«eric»; # single value, still % say join “,”, %pets«eric jimmy»; # hash slice @array[0] = 7; @array[1..7] = (1) xx @array.elems; Perl6: Twenty Things I Love,Five Things You’ll Hate

  37. 5. Sigils: Simplified Array and hash variable names in scalar context automatically produce references. References de-reference without fuss. my $petsref = %pets; say $petsref«eric»; # prints “rat” @allmypets = «rat fish cat»; $petsref«eric» = @allmypets; say $petsref«eric»[1]; # prints “fish” say $petsref.{‘eric’}.[1]; # same thing And why not? It’s no longer ambiguous. Perl6: Twenty Things I Love,Five Things You’ll Hate

  38. 6. Aliasing/Binding operator The infamously confusing use of typeglobs to manage aliasing is dead: # perl 5: my $foo = 5; *bar = \$foo; # perl 6 my $foo = 5; my $bar := $foo; The new aliasing operator is := Perl6: Twenty Things I Love,Five Things You’ll Hate

  39. 6. Aliasing/Binding operator Typeglob aliasing was unclear enough that few people actually did it, except occasionally like so: # perl 5 *$function = sub { print “in $function\n”; }; # perl 6 my &$function := { say “in $function”; }; Needless to say, *foo{CODE},etc is gone. You theoretically never need to directly manipulate the symbol table this way. Perl6: Twenty Things I Love,Five Things You’ll Hate

  40. 6. Aliasing/Binding operator There is a =:= comparison operator: $x := $y; if ( $x =:= $y ) { say “foo!” } # says foo There is also a ::=, which does the binding at compile time. Exporting a function to main: &*::foo := &foo; Note that &foo doesn’t call foo, but returns a reference to it. &foo() or foo() calls foo. Perl6: Twenty Things I Love,Five Things You’ll Hate

  41. 7. Junctions Junctions are a new and extremely powerful datatype. They behave like scalars, but have multiple values: my $allowed = 1|2|3; if ( $val == none( 1..4 ) ) { … } if ( 0 < $x & $y < 10 ) { … } Within the expression, they behave like scalars, but they force the surrounding context to “thread” through all possible variations. my $foo = 1|2 + 3&4; # $foo = ( (4|5) & (5|6) ) Perl6: Twenty Things I Love,Five Things You’ll Hate

  42. 7. Junctions Four forms: all, any, one, none. Each has a functional form of the same name. The first three have infix operator equivalents: any(1,2,3) => 1|2|3 all(“a”,”b”, “c”) => “a” & “b” & “c” one( 1,2 ) => 1 ^ 2 Is this really all that useful? Occasionally. But what is extremely useful is the fact that anywhere you can use a type, you can use a type junction: sub foo(myAnimal&myCat $obj, Int|Str $arg ) {…} Perl6: Twenty Things I Love,Five Things You’ll Hate

  43. 8. Pairs The => operator is no longer a fancy comma. It now constructs a “pair object”, which consists of a key and a value. my $pair = foo => “bar”; Hashes are now essentially an array of pairs, though you aren’t ever forced to care about that. You can build hashes out of pairs, and get pairs back out of hashes. But you can mostly still just treat pairs as a fancy list. Perl6: Twenty Things I Love,Five Things You’ll Hate

  44. 8. Pairs Pairs will likely be used primarily as literals, and mostly as parameters to methods and subs: $result = foo( alpha => $a, $peter, %defaults, bar => 1, ); Note that unlike perl5, these pairs don’t flatten to a list. Pairs and scalars can be intertwined without worry. Perl6: Twenty Things I Love,Five Things You’ll Hate

  45. 8. Pairs Pairs have a second form, often referred to as the “adverbial pair”: foo( :alpha($a) :range[1..10] :text«baz» :bar ); The leading colon eliminates the need for commas, and values default to 1, making boolean options pretty. This form can be mixed with the fat arrow form, and with unnamed arguments. You can’t use non-identifier keys with this form. Perl6: Twenty Things I Love,Five Things You’ll Hate

  46. 8.5. Chained Comparisons Extremely simple, but quite convenient: # perl 5: if (( $x > 0 ) && ( $x < 10 )) { ... } # same, perl 6: if ( 0 < $x < 10 ) { … } The Perl 6 is shorter, but more importantly, it is easier to read and maintain. You can immediately see ranges, and what falls between them. Perl6: Twenty Things I Love,Five Things You’ll Hate

  47. 8.5. Chained Comparisons You aren’t limited to simple 2 or 3 part chains: # perl 5 if (( $a > 0 ) && ( $a < 10 ) && ( $b > 0 ) && ( $b < 10 ) && ( $a == $b )) { ... } # same, perl 6: if ( 0 < $a == $b < 10 ) { ... } The compiler will produce roughly the same bytecode for each, and will be smart enough to short-circuit when possible. Perl6: Twenty Things I Love,Five Things You’ll Hate

  48. 8.5. Chained Comparisons Combine chained comparisons with junctive operators for extremely powerful expressions: # width and height in range? if ( 0 <= all( $w,$h ) <= 500 ) { ... } # anyone care to write the perl5 equivalent # of this? Assume @a, @b Array of Int. if ( all(@a) < none(@b) < all(@a) ) { … } (Note: I am not 100% about that last example…all(*@a),etc., might be required.) Perl6: Twenty Things I Love,Five Things You’ll Hate

  49. 9. for, foreach and loop foreach is gone. The C-like three part for is gone. # In place of for( $i=0; $i < 10; $i++ ) { … } # Use: loop( $i=0; $i < 10; $i++ ) { … } loop { … } # now same as: while( 1 ) { … } for loops now always iterate through a list. Perl6: Twenty Things I Love,Five Things You’ll Hate

  50. 9. for, foreach and loop You can now (if you want) think of for as a function. It takes a list and a closure. It can also take a “pointy” anonymous sub, which looks like this: my $pointy = -> $a, $b { say $a + $b; }; Where $a and $b are now formal parameters to the closure. This leaves us with for loops looking like this: for @nums -> $a, $b { say $a + $b } for @array -> $val { $total += $val } for @a { say; } # $_ aliasing still works Perl6: Twenty Things I Love,Five Things You’ll Hate

More Related