Alexei shilov 2010 rit-rakudo

66
Rakudo Perl 6 What you can do today Jonathan Worthington РИТ++ 2010

Transcript of Alexei shilov 2010 rit-rakudo

Page 1: Alexei shilov 2010 rit-rakudo

Rakudo Perl 6What you can do today

Jonathan WorthingtonРИТ++ 2010

Page 2: Alexei shilov 2010 rit-rakudo

Intro…XXX TODO

Rakudo Perl 6: What You Can Do Today

Page 3: Alexei shilov 2010 rit-rakudo

Perl 6

Rakudo Perl 6: What You Can Do Today

Page 4: Alexei shilov 2010 rit-rakudo

What is Perl 6?

� Take the things that make Perl great…

� Practical - focus on getting the job done

� Multi-paradigm – because there isn't one

approach that's good for all problems

� Linguistic influences – because it's a

programming language

� Making the easy things easy and the hard

things possible

Rakudo Perl 6: What You Can Do Today

Page 5: Alexei shilov 2010 rit-rakudo

What is Perl 6?

� Build a new Perlish language that is…

� More regular – less special cases

� More readable and more maintainable

� More expressive

� More OO, more functional, more

declarative, more parallel…

� Easy things even easier

� Harder things even more in reach

Rakudo Perl 6: What You Can Do Today

Page 6: Alexei shilov 2010 rit-rakudo

1 Specification, Many Implementations

� Unlike Perl 5, Perl 6 has a written language

specification

� No "official" implementation

� Like Perl 5, Perl 6 has a language test suite

� A conforming implementation should pass

the test suite

� A kind of "executable specification"

� Currently around 40,000 tests

Rakudo Perl 6: What You Can Do Today

Page 7: Alexei shilov 2010 rit-rakudo

Rakudo

Rakudo Perl 6: What You Can Do Today

Page 8: Alexei shilov 2010 rit-rakudo

What is Rakudo?

� The most actively developed Perl 6 compiler

� Implements a large portion of the Perl 6

specification (though still some way to go)

� Today passing over 30,000 tests from the

Perl 6 specification test suite (though the

suite is still growing ☺)

� Currently targets the Parrot Virtual Machine

� Later in the year, we plan to target at least

one other platform too

Rakudo Perl 6: What You Can Do Today

Page 9: Alexei shilov 2010 rit-rakudo

How Rakudo Works

� Written in…

� NQP (Bootstrapped subset of Perl 6) Compiler core (grammar and AST construction),

some of the meta-model, module location/loading)

� Perl 6The majority of the built-ins and operators

� Parrot Intermediate LanguageSome lower-level builtins and "glue"

� CDispatchers, signature binder, other VM

customizations and glue

Rakudo Perl 6: What You Can Do Today

Page 10: Alexei shilov 2010 rit-rakudo

How Rakudo Works: Parsing

� First, Rakudo parses your program

� Parser is written in Perl 6 Regexes

� Some scary stuff…

� Might discover new operators during parse

� Have to run BEGIN blocks immediately

Rakudo Perl 6: What You Can Do Today

token statement_control:sym<if> {

<sym> :s

<xblock>

[ 'elsif'\s <xblock> ]*

[ 'else'\s <else=.pblock> ]?

}

Page 11: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

� Whenever we finish parsing something (for

example, an if statement), we run an "action

method"

� This builds an Abstract Syntax Tree

� A representation of the program that is

abstracted away from the language syntax

� Most action methods build up a more

complex AST using smaller bits made by

things we already parsed

Rakudo Perl 6: What You Can Do Today

Page 12: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

Rakudo Perl 6: What You Can Do Today

if $x == 42 { say "The answer!"; }

Page 13: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

Rakudo Perl 6: What You Can Do Today

PAST::Var.new(:name('$x'),:scope('lexical')

)

if $x == 42 { say "The answer!"; }

Page 14: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

Rakudo Perl 6: What You Can Do Today

PAST::Var.new(:name('$x'),:scope('lexical')

)

PAST::Val.new(:value(42)

)

if $x == 42 { say "The answer!"; }

Page 15: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

Rakudo Perl 6: What You Can Do Today

PAST::Op.new(:pasttype('call'),:name('&infix:<==>'),…

)

PAST::Var.new(:name('$x'),:scope('lexical')

)

PAST::Val.new(:value(42)

)

if $x == 42 { say "The answer!"; }

Page 16: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

Rakudo Perl 6: What You Can Do Today

PAST::Op.new(:pasttype('call'),:name('&infix:<==>'),…

)

PAST::Val.new(:value('The Answer!')

)

PAST::Var.new(:name('$x'),:scope('lexical')

)

PAST::Val.new(:value(42)

)

if $x == 42 { say "The answer!"; }

Page 17: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

Rakudo Perl 6: What You Can Do Today

PAST::Op.new(:pasttype('call'),:name('&infix:<==>'),…

)

PAST::Op.new(:pasttype('call'),:name('&say'),…

)

PAST::Val.new(:value('The Answer!')

)

PAST::Var.new(:name('$x'),:scope('lexical')

)

PAST::Val.new(:value(42)

)

if $x == 42 { say "The answer!"; }

Page 18: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

Rakudo Perl 6: What You Can Do Today

PAST::Op.new(:pasttype('call'),:name('&infix:<==>'),…

)

PAST::Block.new( … )

PAST::Op.new(:pasttype('call'),:name('&say'),…

)

PAST::Val.new(:value('The Answer!')

)

PAST::Var.new(:name('$x'),:scope('lexical')

)

PAST::Val.new(:value(42)

)

if $x == 42 { say "The answer!"; }

Page 19: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

Rakudo Perl 6: What You Can Do Today

PAST::Op.new( :pasttype('if'), … )

PAST::Op.new(:pasttype('call'),:name('&infix:<==>'),…

)

PAST::Block.new( … )

PAST::Op.new(:pasttype('call'),:name('&say'),…

)

PAST::Val.new(:value('The Answer!')

)

PAST::Var.new(:name('$x'),:scope('lexical')

)

PAST::Val.new(:value(42)

)

if $x == 42 { say "The answer!"; }

Page 20: Alexei shilov 2010 rit-rakudo

How Rakudo Works: AST Construction

Rakudo Perl 6: What You Can Do Today

PAST::Op.new( :pasttype('if'), … )

PAST::Op.new(:pasttype('call'),:name('&infix:<==>'),…

)

PAST::Block.new( … )

PAST::Op.new(:pasttype('call'),:name('&say'),…

)

PAST::Val.new(:value('The Answer!')

)

PAST::Var.new(:name('$x'),:scope('lexical')

)

PAST::Val.new(:value(42)

)

if $x == 42 { say "The answer!"; }

Page 21: Alexei shilov 2010 rit-rakudo

How Rakudo Works: Code Generation

� We take the AST and produce intermediate

code for the target platform

� Today, we can only produce code in this

stage for the Parrot VM

� Architecture means that we'll be able to add

more backends in the future

� Can also insert optimization and program

analysis phases at any point in the

compilation pipeline

Rakudo Perl 6: What You Can Do Today

Page 22: Alexei shilov 2010 rit-rakudo

Rakudo Perl 6: What You Can Do Today

Examples:What You Can Do Today In

Rakudo

Page 23: Alexei shilov 2010 rit-rakudo

Examples

� We'll look at a range of small, everyday

programming problems and for each one

show…

� The Perl 6 code that solves it

� The output that code gives when run

� Hopefully, a good way for you to start to

grasp some of the new syntax and features

� Show of some cool Perl 6 features ☺

� All examples shown today work in Rakudo

Rakudo Perl 6: What You Can Do Today

Page 24: Alexei shilov 2010 rit-rakudo

say "Hello, world!"

ProblemSay "Hello, world"

Solution

OutputHello, world!

Rakudo Perl 6: What You Can Do Today

Page 25: Alexei shilov 2010 rit-rakudo

print "Enter your name: ";

my $name = $*IN.get;

say "Hi $name!";

ProblemRead input from the console

Solution

OutputEnter your name: Jonathan

Hi Jonathan!

Rakudo Perl 6: What You Can Do Today

Page 26: Alexei shilov 2010 rit-rakudo

loop {

print "Enter a number from 1 to 10: ";

my $num = $*IN.get;

unless 1 <= $num <= 10 { say "Fail!" }

}

ProblemCheck a value is in a given range

Solution 1

OutputEnter a number between 1 and 10: 3

Enter a number between 1 and 10: 42

Fail!

Rakudo Perl 6: What You Can Do Today

Page 27: Alexei shilov 2010 rit-rakudo

loop {

print "Enter a number from 1 to 10: ";

my $num = $*IN.get;

unless $num ~~ 1..10 { say "Fail!" }

}

ProblemCheck a value is in a given range

Solution 2

OutputEnter a number between 1 and 10: 3

Enter a number between 1 and 10: 42

Fail!

Rakudo Perl 6: What You Can Do Today

Page 28: Alexei shilov 2010 rit-rakudo

my @nums = 1, 5, 7, -2, 3, 9, 11, -6, 14;

say [+] @nums;

ProblemAdd up a list of numbers

Solution

Output42

Rakudo Perl 6: What You Can Do Today

Page 29: Alexei shilov 2010 rit-rakudo

my @a = 1, 1, 2, 3, 5, 8;

my @b = 9, 4, 1, 16, 36, 25;

if [<=] @a { say '@a is sorted' }

if [<=] @b { say '@b is sorted' }

ProblemCheck if a list is sorted

Solution

Output@a is sorted

Rakudo Perl 6: What You Can Do Today

Page 30: Alexei shilov 2010 rit-rakudo

my @cities = <Lisbon Tokyo Seoul Riga>;

for @cities -> $city {

say "I've been to $city";

}

ProblemIterate over a list

Solution

OutputI've been to Lisbon

I've been to Tokyo

I've been to Seoul

I've been to Riga

Rakudo Perl 6: What You Can Do Today

Page 31: Alexei shilov 2010 rit-rakudo

my %distances =

Bratislava => 1084,

Stockholm => 442;

for %distances.kv -> $city, $distance {

say "$city is $distance km away";

}

ProblemIterate over the keys and values of a hash

Solution

OutputBratislava is 1084 km away

Stockholm is 442 km away

Rakudo Perl 6: What You Can Do Today

Page 32: Alexei shilov 2010 rit-rakudo

my @a = 75, 47, 90, 22, 80;

my @b = 61, 77, 94, 82, 60;

my @c = 45, 59, 33, 11, 19;

if any(@a) >= 60 { say "Some passes in A" }

if any(@b) >= 60 { say "Some passes in B" }

if any(@c) >= 60 { say "Some passes in C" }

ProblemCheck if any of a list of test scores is a pass

Solution

OutputSome passes in A

Some passes in B

Rakudo Perl 6: What You Can Do Today

Page 33: Alexei shilov 2010 rit-rakudo

my @a = 75, 47, 90, 22, 80;

my @b = 61, 77, 94, 82, 60;

my @c = 45, 59, 33, 11, 19;

if all(@a) >= 60 { say "All passes in A" }

if all(@b) >= 60 { say "All passes in B" }

if all(@c) >= 60 { say "All passes in C" }

ProblemCheck if all of a list of test scores are passes

Solution

OutputAll passes in B

Rakudo Perl 6: What You Can Do Today

Page 34: Alexei shilov 2010 rit-rakudo

my @a = 75, 47, 90, 22, 80;

my @b = 61, 77, 94, 82, 60;

my @c = 45, 59, 33, 11, 19;

if none(@a) >= 60 { say "No passes in A" }

if none(@b) >= 60 { say "No passes in B" }

if none(@c) >= 60 { say "No passes in C" }

ProblemCheck if none of a list of test scores is a pass

Solution

OutputNo passes in C

Rakudo Perl 6: What You Can Do Today

Page 35: Alexei shilov 2010 rit-rakudo

my @drinks = <wine beer vodka>;

say "Tonight I'll drink { @drinks.pick }";

ProblemGet a random item from a list

Solution

Output (results should vary ;-))Tonight I'll drink vodka

Rakudo Perl 6: What You Can Do Today

Page 36: Alexei shilov 2010 rit-rakudo

my @competitors = <Tina Lena Owen Peter>;

my @order = @competitors.pick(*);

for @order { .say }

ProblemShuffle a list into a random order

Solution

Output (results should vary ;-))Peter

Lena

Owen

Tina

Rakudo Perl 6: What You Can Do Today

Page 37: Alexei shilov 2010 rit-rakudo

sub greet($greeting, $name) {

say "$greeting, $name!";

}

greet("hello", "masak");

ProblemWrite and call a subroutine with parameters

Solution

Outputhello, masak

Rakudo Perl 6: What You Can Do Today

Page 38: Alexei shilov 2010 rit-rakudo

sub double(Num $n) { 2 * $n }

say double(21);

say double("oh no I'm not a number");

ProblemWrite a subroutine that only takes a number

Solution

Output42

Parameter type check failed; expected Num,

but got Str for $n in call to double

Rakudo Perl 6: What You Can Do Today

Page 39: Alexei shilov 2010 rit-rakudo

multi double(Num $n) { 2 * $n }

multi double(Str $s) { $s x 2 }

say double(21);

say double("boo");

ProblemUse multi-subs to react differently by type

Solution

Output42

booboo

Rakudo Perl 6: What You Can Do Today

Page 40: Alexei shilov 2010 rit-rakudo

multi fact($n) { $n * fact($n - 1) }

multi fact(0) { 1 }

say fact(1);

say fact(10);

ProblemCompute factorial (recursively)

Solution

Output1

3628800

Rakudo Perl 6: What You Can Do Today

Page 41: Alexei shilov 2010 rit-rakudo

sub fact($n) { [*] 1..$n }

say fact(1);

say fact(10);

ProblemCompute factorial (using a meta-operator)

Solution

Output1

3628800

Rakudo Perl 6: What You Can Do Today

Page 42: Alexei shilov 2010 rit-rakudo

sub postfix:<!>($n) { [*] 1..$n }

say 1!;

say 10!;

ProblemAdd a new factorial operator (so 10! works)

Solution

Output1

3628800

Rakudo Perl 6: What You Can Do Today

Page 43: Alexei shilov 2010 rit-rakudo

class Product {

has $.name; # Attr + accessor

has $!price; # Attr only

has $.discount is rw;

# Attr + lvalue accessor

method get_price {

return $!price - $!discount;

}

}

ProblemDeclare a class with attributes and a method

Solution

Rakudo Perl 6: What You Can Do Today

Page 44: Alexei shilov 2010 rit-rakudo

my $prod = Product.new(

name => "Beer",

price => 500,

discount => 60

);

say $prod.get_price;

ProblemInstantiate a class and call a method on it

Solution

Output440

Rakudo Perl 6: What You Can Do Today

Page 45: Alexei shilov 2010 rit-rakudo

say $prod.name;

$prod.discount = 40;

say $prod.get_price;

$prod.name = 'Wine';

ProblemGet/set attributes through accessors

Solution

OutputBeer

460

Cannot assign to readonly variable.

Rakudo Perl 6: What You Can Do Today

Page 46: Alexei shilov 2010 rit-rakudo

my @products =

Product.new(name => 'Beer', price => 500),

Product.new(name => 'Wine', price => 450),

Product.new(name => 'Vodka', price => 1600);

my @uc_names = @products>>.name>>.uc;

for @uc_names { .say }

ProblemCall a method on every object in a list

Solution

OutputBEER

WINE

VODKA

Rakudo Perl 6: What You Can Do Today

Page 47: Alexei shilov 2010 rit-rakudo

my @meths = Product.^methods(:local);

for @meths>>.name { .say }

ProblemIntrospect a class to find its methods

Solution

Outputget_price

discount

name

Rakudo Perl 6: What You Can Do Today

Page 48: Alexei shilov 2010 rit-rakudo

my @products =

Product.new(name => 'Beer', price => 500),

Product.new(name => 'Wine', price => 450),

Product.new(name => 'Vodka', price => 1600);

my @sorted = @products.sort(*.name);

for @sorted { .name.say }

ProblemSort an array of objects by result of a method

Solution (Example 1)

Output (Example 1)Beer

Vodka

Wine

Rakudo Perl 6: What You Can Do Today

Page 49: Alexei shilov 2010 rit-rakudo

my @products =

Product.new(name => 'Beer', price => 500),

Product.new(name => 'Wine', price => 450),

Product.new(name => 'Vodka', price => 1600);

my @sorted = @products.sort(*.get_price);

for @sorted { .name.say }

ProblemSort an array of objects by result of a method

Solution (Example 2)

Output (Example 2)Wine

Beer

Vodka

Rakudo Perl 6: What You Can Do Today

Page 50: Alexei shilov 2010 rit-rakudo

my @temperatures = -3, 5, 7, 2, -1, -4, 0;

say "Minimum was " ~ @temperatures.min;

say "Maximum was " ~ @temperatures.max;

ProblemFind minimum and maximum values from a list

Solution (Example 1)

Output (Example 1)Minimum was –4

Maximum was 7

Rakudo Perl 6: What You Can Do Today

Page 51: Alexei shilov 2010 rit-rakudo

my @products =

Product.new(name => 'Beer', price => 500),

Product.new(name => 'Wine', price => 450),

Product.new(name => 'Vodka', price => 1600);

say "Cheapest: " ~ @products.min(*.get_price).name;

say "Costliest: " ~ @products.max(*.get_price).name;

ProblemFind minimum and maximum values from a list

Solution (Example 2)

Output (Example 2)Cheapest: Wine

Costliest: Vodka

Rakudo Perl 6: What You Can Do Today

Page 52: Alexei shilov 2010 rit-rakudo

class Paper { }

class Scissor { }

class Stone { }

multi win(Paper, Stone) { "Win" }

multi win(Scissor, Paper) { "Win" }

multi win(Stone, Scissor) { "Win" }

multi win(::T, T) { "Draw" }

multi win(Any, Any) { "Lose" }

ProblemPaper, Scissor, Stone game

Solution (Part 1)

Rakudo Perl 6: What You Can Do Today

Page 53: Alexei shilov 2010 rit-rakudo

say win(Paper, Paper);

say win(Scissor, Stone);

say win(Stone, Scissor);

ProblemPaper, Scissor, Stone game

Solution (Part 2)

OutputDraw

Lose

Win

Rakudo Perl 6: What You Can Do Today

Page 54: Alexei shilov 2010 rit-rakudo

Rakudo *

Rakudo Perl 6: What You Can Do Today

Page 55: Alexei shilov 2010 rit-rakudo

What is Rakudo *?

� Rakudo is making great progress

� Steadily implementing more of the spec

� Steadily passing more and more tests

� Fixing lots of bugs

� Number of active developers is growing

� So far we have been very much focused on

building Rakudo

� Rakudo * is a release where we instead

focus on what early adopters need

Rakudo Perl 6: What You Can Do Today

Page 56: Alexei shilov 2010 rit-rakudo

What will the release include?

� We make a compiler release every month;

by contrast, Rakudo * is a distribution

release including:

� The Rakudo compiler, of course ☺

� Tool for downloading, installing and

updating modules

� A range of Perl 6 modules that help you

achieve some common tasks (e.g. HTTP

client/server, database connectivity, web

things, YAML…)

Rakudo Perl 6: What You Can Do Today

Page 57: Alexei shilov 2010 rit-rakudo

What will the release include?

� We are also aiming to include a couple of

other projects…

� Zavolaj! – a module that lets you write

some basic pure Perl 6 bindings to C

libraries; we built a MySQL client with it

� Blizkost – a Perl 5 � Parrot bridge layer

that will allow you to use Perl 5 modules

from within Perl 6

Rakudo Perl 6: What You Can Do Today

Page 58: Alexei shilov 2010 rit-rakudo

What Rakudo * Will Do Well

� Rakudo has good coverage of a lot of the

Perl 6 specification…

� Wide range of built-in operators, types and

functions

� Subs, signatures and multiple dispatch

� Object orientation, including classes,

roles, introspection and much more

� Perl 6 regexes and grammars (it's the

same engine we use to parse Perl 6!)

Rakudo Perl 6: What You Can Do Today

Page 59: Alexei shilov 2010 rit-rakudo

Weaker Areas

� Rakudo * will have a lot to offer, and should

be useful for a range of tasks

� However, it's not The Full Perl 6, and of

course has some weak spots, including:

� Missing support for threading

� No native types support

� Fairly slow – not much work on

optimization yet, and no optimizer

Rakudo Perl 6: What You Can Do Today

Page 60: Alexei shilov 2010 rit-rakudo

When?

� Soon ☺

Rakudo Perl 6: What You Can Do Today

Page 61: Alexei shilov 2010 rit-rakudo

When?

� Soon ☺

� Either late May or early-mid June

Rakudo Perl 6: What You Can Do Today

Page 62: Alexei shilov 2010 rit-rakudo

When?

� Soon ☺

� Either late May or early-mid June

� Yes, this year

Rakudo Perl 6: What You Can Do Today

Page 63: Alexei shilov 2010 rit-rakudo

Get Involved!

Rakudo Perl 6: What You Can Do Today

Page 64: Alexei shilov 2010 rit-rakudo

Want to learn more?

� Get Rakudo Perl 6 from:

http://www.rakudo.org/

� Lots of Perl 6 resources can be found at:

http://www.perl6.org/

� Join the friendly IRC channel:

#perl6 on irc.freenode.org

� Write modules, write applications, jump into

the evolving Perl 6 community and make

your mark on it ☺

Rakudo Perl 6: What You Can Do Today

Page 65: Alexei shilov 2010 rit-rakudo

Thank You

Rakudo Perl 6: What You Can Do Today

Page 66: Alexei shilov 2010 rit-rakudo

Questions?

Rakudo Perl 6: What You Can Do Today