Perl6

Anton Berezin

tobez@tobez.org

 

About

This talk is about

 

Perl6, the language

 

 

 

Perl 6 is the next version of the Perl programming language.

Why Perl5 is not good enough

 

 

The internals of the version 5 interpreter are so tangled that they hinder maintenance, thwart some new feature efforts, and scare off potential internals hackers.

The language as of version 5 has some misfeatures that are a hassle to ongoing maintenance of the interpreter and of programs written in Perl.

Perl6 design

Damian Conway:

 

The Perl 6 design process is about keeping what works in Perl 5, fixing what doesn't, and adding what's missing.

Perl6 vision

 

 

The vision for Perl 6 is more than simply a rewrite of Perl 5. By separating the parsing from the compilation and the runtime, we're opening the doors for multiple languages to cooperate.

Perl6 vision

 

 

You'll be able to write your program in Perl 6, Perl 5, TCL, Python, or any other language that there's a parser written for.

Interchangable runtime engines let you interpret your bytecode or convert it to something else.

Community participation

Larry Wall:

 

Perl 5 was my rewrite of Perl. I want Perl 6 to be the community's rewrite of Perl and of the community.

Requests for Change

 

In the beginning, there were RFCs.

The initial design phase of perl6 was to produce suggestions for Larry to evaluate.

Every idea that someone wants in Perl should have had an RFC produced for it.

RFCs

 

 

The community brainstorming process finished
August 1, 2000, resulting in 361 RFCs.

Larry Wall:
If you ask for RFCs from the general public, you get a lot of interesting but contradictory ideas, because people tend to stake out polar positions, and none of the ideas can build on each other.

Language needs a designer

 

 

Apocalypses

 

 

Apocalypses are initial design document written over time by Larry. Nowadays they are largely replaced with Synopses.

Exegeses

 

 

Exegeses were written by Damian Conway and were explaining Apocalypses in more detail and with more examples. Nowadays they are also largely replaced with Synopses.

Synopses

 

 

Synopses are short, up to the point Perl6 design documents. They are kept current, so if an Apocalypse or an Exegesis disagrees with a Synopsis, the Synopsis wins.

Multi-purpose

 

 

Perl6 is supposed to be good for

Compatibility

 

 

Perl6 will be able to execute Perl5
code. In fact, the interpreter will
assume that it is being fed Perl 5
code unless specified otherwise.

Compatibility

 

 

This gives us the access to the wealth of CPAN.

10+ years

2800+ authors

10000+ modules

Huffman coding

 

 

Perl5's $o->method becomes $o.method

Since "." is occupied, "~" becomes string concatenation operator.

Huffman coding

 

 

Perl5's $d = $a ? $b : $c becomes
$d = $a ?? $b :: $c

Which frees up "?" and ":".

Colon

 

 

Larry's First Law of
Language Redesign:

 

Everyone wants the colon.

OO or not?

 

 

If you want to treat everything as
objects in Perl 6, Perl will help
you do that. If you don't want to
treat everything as objects, Perl
will help you with that viewpoint as well.

Various Perl5 modules emulating Perl6

You can try those:

 

Perl6-Attributes Perl6-Bible Perl6-Binding Perl6-Builtins Perl6-Classes Perl6-Contexts Perl6-Currying Perl6-Export Perl6-Export-Attrs Perl6-Form Perl6-Gather Perl6-Interpolators Perl6-Junction Perl6-Parameters Perl6-Placeholders Perl6-Roles Perl6-Rules Perl6-Say Perl6-Slurp Perl6-Subs Perl6-Tokener Perl6-Variables

 

...from your nearest CPAN mirror.

Parrot

 

 

Parrot is a virtual machine designed to
efficiently compile and execute bytecode
for interpreted languages. Parrot will be
the target for the final Perl 6 compiler, and is already usable as a backend for Pugs, as well as variety of other languages.

Parrot

In other words, parrot is a
Just to give you an idea.

Target languages

 

 

Amber for parrot BASIC/compiler
BASIC/interpreter bc befunge bf Cardinal Cola Common Lisp conversion forth Jako lazy-k Lua Monkey Parrot m4 miniperl Ook OpenComal Parakeet parrot_compiler Pint Pugs Punie Python regex Ruby Span Scheme Tcl unlamba URM YAL Zcode

 

Degree of completeness vary. To say the least.

Performance

 

Parrot has several different
"runcores", including JIT
for some platforms.

Some of the runcores are fast.

Benchmarks

Straightforward calculation of prime numbers
up to 50000:

A note of caution

 

The results of those benchmarks should
be interpreted with care.

 

But there is a hope for Parrot.

Pugs

Pugs is an implementation of Perl 6,
written in Haskell. It aims to implement
the full Perl6 specification,
as detailed in the Synopses.

 

Started in February 2005.

Haskell

Haskell is a standardized, purely functional
programming language with built-in lazy
evaluation capabilities. While there are
several different implementations available,
currently Pugs needs to be built with GHC,
because it uses several GHC-specific features.

GHC is a state-of-the-art compiler and interactive environment, available under a BSD-style license. Itself written in Haskell, GHC can compile Haskell to bytecode, C code, and machine code on some platforms.

Speed

The parser part of Pugs is very fast,
due to its robust underpinning in Parsec.

However, the Pugs evaluator is currently
not optimized at all: dispatching is around
1000 operators per second on a typical PC, which is nearly 100 times slower than Perl 5. Still, it is fast enough for prototyping language features.

Short history

Started in February 2005 by Autrijus Tang
(now Audrey Tang).

Short history

 

Short history

-Ofun

 

Anarchistic development model

 

~9000 commits

 

120+ committers

Feature line

 

Can use Perl5 and JS libraries

 

use perl5:DBI;

 

use jsan:DOM;

Javascript backend demo

 

 

Select language features

 

Everything is an object

 

 


say "Hello, world".reverse;
say 3.as("%03d");
say <a b c>.elems;

Scalar context(s)

Unary ~ now imposes a string context on
its argument, and + imposes a numeric
context (as opposed to being a no-op in
Perl 5). Along the same lines, ? imposes
a boolean context, and * imposes a list
context.
Perl5:
$len = scalar(@a);
$str = "@a";

Perl6:

$len = +@a;
$str = ~@a;

Hyper operators

 

>> and << are used to denote "list operations", which operate on each element of two lists (or arrays) and return a list (or array) of the results.

 


(1,1,2,3,5) >>+<< (1,2,3,5,8);  # (2,3,5,8,13)

Hyper operators

 

Also unary:

 


@negatives = -<< @positives;

Reduction operators

 

Any binary operator can be surrounded by square brackets to create a list operator that reduces using that operation:

 


[+] 1, 2, 3;   # 1 + 2 + 3 = 6

Smart match

 

~~ replaces =~, but is much more general. A few examples:

 


$s ~~ /re/   just like Perl5's $s =~ /re/
%h1 ~~ %h2   match if %h1.keys.sort eq %h2.keys.sort
%h ~~ @a     match if %h{any(@a)}
@a ~~ "str"  match if any(@a) eq "str";

Switch statement

 


given EXPR {
  when EXPR { ... }
  when EXPR { ... }
  default { ... }
}

 

"given" is just one of the ways to set topic ($_).
"when" does smartmatching of the $_ with EXPR.

Exception handling

 

Exceptions are ignored:
try {
   ...
}
Exceptions are handled by CATCH:
try {
   ...
   CATCH {
      when ... {...}
      ...
   }
}

Exception handling

"try" is actually redundant, any block can have CATCH:

{
   ...
   CATCH {
      when ... {...}
      ...
   }
}

Rules

 

The following elements are the same
as in Perl5 regexes:

Rules

 

Rules

 

Rules

 

Extensible metasyntax

 

Named captures access

 


if / <ident> = <ident> / {
   $left_side = $<ident>[0];
   $right_side = $<ident>[1];
}

Junctions

|, &, and ^ are no longer bitwise operators
but now serve a much higher cause:
they are now the junction constructors.
A junction is a single value that is equivalent
to multiple values.

 

This allows one to write things like:
my $a = 4;
if $a < 3|4|5 { say "less" }
if $a > 3|4|5 { say "greater" }

Junctions demo

 

 

Junctions

 

Junctions thread through operations,
returning another junction:

 


(1|2|3) + 4;      # 5|6|7
(1|2) + (3&4);    # (4|5) & (5|6)

Chained comparisons

 

One can now write:

 


if $x1 <= $x <= $x2 { say "in range" }

Binding (aliasing)

 

Perl5:
$x = "Hello";
*y = *x;
$y = "Bye!";
print "$x\n";

 

Perl6:
our $x = "Hello";
our $y := $x;
$y = "Bye!";
say $x;

Arguments and formal parameters

 

Proper parameter lists:

  sub fact ($n)
  {
     return 1 if $n < 2;
     return $n*fact($n - 1);
  }
  say fact(5);

Factorial demo

 

 

Arguments and formal parameters

 

Also named:

  sub my_substr ($str, $from?, $len?)
  {
    substr($str, $from||0, $len||Inf)
  }
  say my_substr("hello", len => 4);

Arguments and formal parameters

 

Also with default values:

  sub my_substr ($str, $from = 0, $len = Inf)
  {
    substr($str, $from, $len)
  }
  say my_substr("hello", from => 2);

Pointy subs

 

Almost a synonym for the anonymous sub
(parameter list does not require
parentheses):
$sq = -> $val { $val**2 };
# Same as: $sq = sub ($val) { $val**2 };

for @list -> $elem {
    print "$elem\n";
}
# Same as: for @list, sub ($elem) {
#              print "$elem\n";
#          }

Pointy subs

 

But behaves like a block:

for @list -> $elem {
    print "$elem\n";
    return if $elem ~~ /done/;
}
# for @list, sub ($elem) {
#    print "$elem\n";
#    return if $elem ~~ /done/;  # OOPS
# }

Zip

In order to support parallel iteration over
multiple arrays, Perl 6 has a zip function
that builds tuples of the elements of two or
more arrays.

 


    for zip(@names; @codes) -> [$name, $zip]
    {
        print "Name: $name;   Zip code: $zip\n";
    }

Each

Or use each if you want flat list as opposed to tuples:

 


for each(@names; @codes) -> $name, $zip
{
    print "Name: $name;   Zip code: $zip\n";
}

Placeholder arguments

Standard sub declaration:

$func = sub ($a, $b) { print if $a eq $b };
A "pointy" sub:

$func = -> $a, $b { print if $a eq $b };
Placeholder arguments:

$func = { print if $^a eq $^b }

 

Sorted!

Pipes


grep { $_ % 2 } @data;

grep { $_ % 2 } <== @data;

@data ==> grep { $_ % 2 };

Pipes


@oddsquares = map { $_**2 },
        sort grep { $_ % 2 }, @nums;

# more clearly written as...
@oddsquares = map { $_**2 } <==
              sort <==
              grep { $_ % 2 } <== @nums;

@oddsquares = (@nums ==> grep { $_ % 2 } ==>
               sort ==> map { $_**2 });

Pipes



# push semantics

my @foo;
0..2       ==> @foo;
'a'..'c'   ==> @foo;

say @foo;   # 0,1,2,'a','b','c'

Introspection

 

Every class object has a .meta
method that lets you get at
the class's metaclass object,
which lets you get at all
the metadata properties for
the class.

Perspective

 

 

Questions

 

 

Any questions?

Resources

 

 

http://dev.perl.org/
http://www.parrotcode.org/
http://www.pugscode.org/

The End

 

 

Thank you!