80
Per l6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – [email protected] Oct. 21/2004

Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – [email protected]. 21/2004

Embed Size (px)

Citation preview

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

Perl6Twenty Things I Love,Five Things You’ll HateEric Maki – [email protected] Oct. 21/2004

Page 2: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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 [email protected].

-- Eric Maki

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 3: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 4: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 5: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 6: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 7: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 8: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 9: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

1. Backwards Compatibility

MitigationFortunately, 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

Page 10: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 11: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 12: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 13: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

2. Complexity

Mitigation 2Perl6 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

Page 14: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 15: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 16: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 17: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 18: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 19: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 20: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 21: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 22: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 23: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 24: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 25: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 26: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 27: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 28: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 29: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 30: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 31: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

3. Everything is an Object, Sort Of

Example: Strings

No more scalar() vs. length() confusion:[email protected]@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

Page 32: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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 ( [email protected] ) { … } # replaces @#foo

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 33: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 34: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 35: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

4. Different Things are More Different

In Perl 6Different 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

Page 36: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 37: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 38: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 39: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 40: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 41: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 42: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 43: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 44: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 45: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 46: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 47: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

8.5. Chained Comparisons

You aren’t limited to simple 2 or 3 part chains:

# perl 5if (( $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

Page 48: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 49: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 50: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

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

Page 51: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

10. zip operator

The zip operator ¥ or function zip() takes arrays and interleaves them:

my @wholenums := zip( @odds, @evens );

Combine this with the new for:

for zip(@a, @b) -> $a, $b { push @c, $a + $b; }

for @x ¥ @y ¥ @values -> $x, $y, $val{ @matrix[ $x ][ $y ] = $val; }

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 52: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

11. Hyperoperators

Any operator, method or sub can be “hyperized”, meaning that it will autothread over all its arguments:

# binary:

( 1,1,2,3 ) »+« ( 1,2,3,5 ); # ( 2,3,5,8 )

( 3,8,2,9 ) »-« 1; # ( 2,7,1,8 )

# unary:

@negatives = -« @positives;

@objects ».foo();

@foo »++; # same as: for @foo { $_++; }

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 53: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

11. Pipelines

==> and <== are the new pipeline operators:

# perl 5 Schwartzian Transform:@sortedbysize = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, -s] } @files;

# perl 6 Schwartzian Transform:@sortedbysize <== map { $_->[0] } <== sort { $^a->[1] <=> $^b->[1] } <== map { [$_, -s] } <== @files;

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 54: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

11. Pipelines

Or:

# perl 6 Schwartzian Transform:@files ==> map { [ $_, -s ] } ==> sort { $^a->[1] <=> $^b->[1] }

==> map { $_->[0] } ==> @sortedbysize;

As <==, pipelines are mostly just visual sugar. But ==> reverses the normal series of list operators, so that you don’t have to read them backwards.

This is a minor language tweak that increases the readability of pipelined operations 100%.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 55: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

12. Smart match operator

The match binding operator =~ is superseded by the smart match operator ~~.

Smartmatch does what =~ did, but considerably more:

$str ~~ m/bar/; # does =~

$num ~~ 1; # does ==

$str ~~ “foo” # does eq

# does =~, == or eq, depending on runtime types:

$unknown ~~ $foo

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 56: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

12. Smart match operator

Smart match does something reasonable for pretty much any pair of things you toss at it… but it makes those determinations based on runtime times.

$obj ~~ ::MyFoo|Int; # if $obj.does(MyFoo|Int)

@arr1 ~~ m/foo/; # if any elems match Rule

%hash ~~ .{$foo}; # if subscript exists

$obj ~~ .somemeth() # if method returns true

$foo ~~ { chomp; } # if closure returns true

And so on… (the synopsis lists some 3 dozen combinations – most of them intuitive.)

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 57: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

12. Smart match operator

And naturally you can still do nonsense like this:

%pairs = $foo ~~ m:w:g/(<ident>+) = (<ident>+)/;

@numbers = $input ~~ m:g{ (\d+) };

Generally you will use ~~ in boolean context, but most of the different combinations will be smart enough to return things in list and numeric contexts.

If not, you can rewrite them.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 58: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

13. given and when

Perl finally gets a switch mechanism… but it also gets a lot more.

given ( $foo )

{

when m/^\d+[.\d+]?/ { say “foo is a number”; }

when Int { say “foo is an Int”; }

default { say “foo is a thing”; }

}

given sets the topic, and when runs a smart match against it. You don’t have to use them together.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 59: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

13. given and when

for ( @foo ) { # loopy switch

when Int { $_ %= 2; continue; }

when 1 { say “odd”; }

when 0 { say “even”; }

default { say “Not a number”; }

}

given ( $input ) { # topicalization

chomp;

s:g/foo[bar]?//;

say;

}

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 60: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

13. given and when

Because when can test the runtime type of a variable, and since exceptions are objects…

try { something_fragile(); CATCH { when X::Overflow { … } when X::MyTimeOut { … } default { … } }}

And that is how exception handling works*

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 61: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

13. given and when

One last cool given/when example:

given want

{

when Scalar {...} # called in scalar context

when List {...} # called in list context

when Lvalue {...} # lvalue expected

when 2 {...} # two values wanted

}

want() replaces the the poorly named wantarray() It now returns an object with properties.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 62: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

14. temp, state and let

local is gone… or at least renamed to something a little clearer: temp.

More interesting is let: It does a temp on a variable, but doesn’t reset if the block exits successfully. This is a “hypothetical variable”:

my $fetch = sub( $x is rw, URL $url ) {

let $x = fetch_url( $url );

$x.header ~~ m{ Not Found } && fail;

}

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 63: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

14. temp, state and let

My favourite is state. This declares a persistent lexical. (persistent over the life of a closure.)

my $counter = { state $cnt = 0;

return $cnt++; };

say $counter(); # 0

my $cnt2 = &$counter; # clone

say $counter(); # 1

say $cnt2(); # 1

Cloned closures have their own states.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 64: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

15. Separate OO Syntax

A class is no longer a package, a method no longer a sub:

class Dog is Animal { has $.name; # accessor methods generated has $:bones; # private to class

method bark () { say “Woof”; } method feed ( $self: Int $bones ) {

$:bones += $bones; } method dig () { $:bones--; }}

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 65: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

15. Separate OO Syntax

Larry takes 60+ pages to explain Perl6 OO in Apocalypse 12, and there is a lot of depth and nuance to it.

However, the short story is that it is much more powerful and much more simple than Perl5 OO.

Inheritance:

class Dog is Mammal {...}

Private Methods:

method :think ( Brain $self: $thought ) { … }

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 66: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

15. Separate OO Syntax

Instance variables are easy:

class Foo {

has Str $.name is rw; # public rw

has Int $:id; # private

}

# Implicit contructor:

my $foo1 = Foo.new( :name«Eric» );

# Implicit lvalue accessor:

$foo1.name .= “ Maki”;

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 67: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

15. Separate OO Syntax

Class vs Instance methods:

class Foo {

method generate( Class $class: *@args )

{ … }

# class or instance method:

method clone( Class|Foo $thing ) { … }

}

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 68: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

15. Separate OO Syntax

If you omit the invocant in the method parameters, it defaults to $_.

Conveniently .method() defaults to call a method of $_.

There is a lot of depth and brilliance to Perl6 OO, but it still feels like Perl… probably moreso than Perl5 OO.

It is easy to do easy things, possible to do nearly everything.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 69: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

16. Parameters

As you likely noticed with the methods, you now have much less minimal subroutine parameter/signature system.

sub foo { say for @_ } # still works

# greedy parameter:

sub foo ( *@args ) { say for @args }

# strict parameters:

sub repeat ( Int $cnt, Str $text )

{ say $text for 1..$cnt; }

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 70: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

16. Parameters

# required, named parameters

sub foo ( $head, $tail )

{ say “$head $tail”; }

foo( ‘foo’, ‘bar’ );

foo( :tail«bar» :head«foo» );

foo( tail => ‘bar’, head => ‘foo’ );

# all print “foo bar”

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 71: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

16. Parameters

# optional parameters:

sub foo ( $x, ?$y ) { … }

# default values:sub foo ( $str, ?$len = 10 ) { … }

# explicitly name values:sub foo ( $str, +$chomp = 0, +$verbose ) { chomp $str if $chomp; say $str if $verbose;}foo( “bar”, :verbose :chomp ); # must be named

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 72: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

16. multi subs and methods

This parameter system lets us distinguish subs and methods with the same name:

multi sub feed ( Dog $fido ) { … }multi sub feed ( Cat $mittens ) { … }

class Puppy { multi method feed ( Chow $food, Int $quant ) {…} multi method feed ( Chow $food ) { … }}

subs and methods are dispatched at run time based on best parameter match.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 73: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

17. overloading

This opens the way for operator overloading that doesn’t suck:

multi sub *infix:+ (Dog $x, Cat $y) {...}

multi sub *infix:+ (Complex $z, Real $x)

returns Complex

{

my Complex $ret = $z;

$ret.real += $x;

return $ret;

}

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 74: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

18. Properties/Traits

Properties/traits are flexible units of behaviour. Some are built in, but you can build your own.

Some interesting built-ins:

my $x is constant = 3;return “404” but false;sub foo ( $x is rw ) { … }sub factorial ( Int $x ) is cached { … }

Inheritance and exporting will be properties rather than shoehorned into use.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 75: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

19. Regex Overhaul

Regexen… or rules as they are now called, get a complete reworking in Perl6. Much more than the rest of Perl, regexen were weighed down by history from sed, grep and ed.

Perl 6 rules are easily a 2 hour talk on their own, but here are some quick notes:

Modifiers come to front of regex:m:g{ foo | bar } # x is now default

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 76: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

19. Regex Overhaul

Rules can be defined like subs:rule ident { [<alpha>|_] \w* }

Then used as assertions:m:w{ sub <ident> \( \) };

[] now group without capture like (?:).rules can be gathered into grammars.Easily bind captured groups to named variables.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 77: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

19. Regex Overhaul

From s05: Perl 6 will come with at least one grammar predefined:

grammar Perl { # Perl's own grammar

rule prog { <line>* }

rule line { <decl>

| <loop>

| <label> [<cond>|<sideff>|;]

}

rule decl { <sub> | <class> | <use> }

# etc. etc.

}

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 78: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

19. Regex Overhaul

Hence:

given $source_code {

$parsetree = m/<Perl.prog>/;

}

Yes. The Perl6 compiler will be written in Perl6.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 79: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

20. More Perl than Perl5

In flipping through all the example Perl6 code that accompanies all that documentation, one thing occurred to me: It all looked and felt like perl.

If perhaps there is a Platonic Ideal of Perl, it seems clear that Perl5 was a rough approximation.

Perl6 seems like a much closer approximation.

Perl6: Twenty Things I Love,Five Things You’ll Hate

Page 80: Perl6 Twenty Things I Love, Five Things You’ll Hate Eric Maki – eric@uc.orgOct. 21/2004

20. More Perl than Perl5

Larry has suggested that Perl6 is a huge leap forward towards this perfect-Perlness.

He calls that perfect Perlness: Perl7.

Perl6: Twenty Things I Love,Five Things You’ll Hate