4. Rakudo Perl 6: What You Can Do Today
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
5. Rakudo Perl 6: What You Can Do Today
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
6. Rakudo Perl 6: What You Can Do Today
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
8. Rakudo Perl 6: What You Can Do Today
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
9. Rakudo Perl 6: What You Can Do Today
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 6
The majority of the built-ins and operators
Parrot Intermediate Language
Some lower-level builtins and "glue"
C
Dispatchers, signature binder, other VM
customizations and glue
10. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: Parsing
First, Rakudo parses your program
Parser is written in Perl 6 Regexes
token statement_control:sym<if> {
<sym> :s
<xblock>
[ 'elsif's <xblock> ]*
[ 'else's <else=.pblock> ]?
}
Some scary stuff…
Might discover new operators during parse
Have to run BEGIN blocks immediately
11. Rakudo Perl 6: What You Can Do Today
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
12. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: AST Construction
if $x == 42 { say "The answer!"; }
13. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: AST Construction
if $x == 42 { say "The answer!"; }
PAST::Var.new(
:name('$x'),
:scope('lexical')
)
14. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: AST Construction
if $x == 42 { say "The answer!"; }
PAST::Var.new( PAST::Val.new(
:name('$x'), :value(42)
:scope('lexical') )
)
15. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: AST Construction
if $x == 42 { say "The answer!"; }
PAST::Op.new(
:pasttype('call'),
:name('&infix:<==>'),
…
)
PAST::Var.new( PAST::Val.new(
:name('$x'), :value(42)
:scope('lexical') )
)
16. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: AST Construction
if $x == 42 { say "The answer!"; }
PAST::Op.new(
:pasttype('call'),
:name('&infix:<==>'),
…
)
PAST::Var.new( PAST::Val.new(
:name('$x'), :value(42)
:scope('lexical') )
) PAST::Val.new(
:value('The Answer!')
)
17. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: AST Construction
if $x == 42 { say "The answer!"; }
PAST::Op.new(
:pasttype('call'),
:name('&infix:<==>'),
… PAST::Op.new(
) :pasttype('call'),
:name('&say'),
…
PAST::Var.new( PAST::Val.new( )
:name('$x'), :value(42)
:scope('lexical') )
) PAST::Val.new(
:value('The Answer!')
)
18. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: AST Construction
if $x == 42 { say "The answer!"; }
PAST::Op.new( PAST::Block.new( … )
:pasttype('call'),
:name('&infix:<==>'),
… PAST::Op.new(
) :pasttype('call'),
:name('&say'),
…
PAST::Var.new( PAST::Val.new( )
:name('$x'), :value(42)
:scope('lexical') )
) PAST::Val.new(
:value('The Answer!')
)
19. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: AST Construction
if $x == 42 { say "The answer!"; }
PAST::Op.new( :pasttype('if'), … )
PAST::Op.new( PAST::Block.new( … )
:pasttype('call'),
:name('&infix:<==>'),
… PAST::Op.new(
) :pasttype('call'),
:name('&say'),
…
PAST::Var.new( PAST::Val.new( )
:name('$x'), :value(42)
:scope('lexical') )
) PAST::Val.new(
:value('The Answer!')
)
20. Rakudo Perl 6: What You Can Do Today
How Rakudo Works: AST Construction
if $x == 42 { say "The answer!"; }
PAST::Op.new( :pasttype('if'), … )
PAST::Op.new( PAST::Block.new( … )
:pasttype('call'),
:name('&infix:<==>'),
… PAST::Op.new(
) :pasttype('call'),
:name('&say'),
…
PAST::Var.new( PAST::Val.new( )
:name('$x'), :value(42)
:scope('lexical') )
) PAST::Val.new(
:value('The Answer!')
)
21. Rakudo Perl 6: What You Can Do Today
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
22. Rakudo Perl 6: What You Can Do Today
Examples:
What You Can
Do Today In
Rakudo
23. Rakudo Perl 6: What You Can Do Today
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
24. Rakudo Perl 6: What You Can Do Today
Problem
Say "Hello, world"
Solution
say "Hello, world!"
Output
Hello, world!
25. Rakudo Perl 6: What You Can Do Today
Problem
Read input from the console
Solution
print "Enter your name: ";
my $name = $*IN.get;
say "Hi $name!";
Output
Enter your name: Jonathan
Hi Jonathan!
26. Rakudo Perl 6: What You Can Do Today
Problem
Check a value is in a given range
Solution 1
loop {
print "Enter a number from 1 to 10: ";
my $num = $*IN.get;
unless 1 <= $num <= 10 { say "Fail!" }
}
Output
Enter a number between 1 and 10: 3
Enter a number between 1 and 10: 42
Fail!
27. Rakudo Perl 6: What You Can Do Today
Problem
Check a value is in a given range
Solution 2
loop {
print "Enter a number from 1 to 10: ";
my $num = $*IN.get;
unless $num ~~ 1..10 { say "Fail!" }
}
Output
Enter a number between 1 and 10: 3
Enter a number between 1 and 10: 42
Fail!
28. Rakudo Perl 6: What You Can Do Today
Problem
Add up a list of numbers
Solution
my @nums = 1, 5, 7, -2, 3, 9, 11, -6, 14;
say [+] @nums;
Output
42
29. Rakudo Perl 6: What You Can Do Today
Problem
Check if a list is sorted
Solution
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' }
Output
@a is sorted
30. Rakudo Perl 6: What You Can Do Today
Problem
Iterate over a list
Solution
my @cities = <Lisbon Tokyo Seoul Riga>;
for @cities -> $city {
say "I've been to $city";
}
Output
I've been to Lisbon
I've been to Tokyo
I've been to Seoul
I've been to Riga
31. Rakudo Perl 6: What You Can Do Today
Problem
Iterate over the keys and values of a hash
Solution
my %distances =
Bratislava => 1084,
Stockholm => 442;
for %distances.kv -> $city, $distance {
say "$city is $distance km away";
}
Output
Bratislava is 1084 km away
Stockholm is 442 km away
32. Rakudo Perl 6: What You Can Do Today
Problem
Check if any of a list of test scores is a pass
Solution
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" }
Output
Some passes in A
Some passes in B
33. Rakudo Perl 6: What You Can Do Today
Problem
Check if all of a list of test scores are passes
Solution
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" }
Output
All passes in B
34. Rakudo Perl 6: What You Can Do Today
Problem
Check if none of a list of test scores is a pass
Solution
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" }
Output
No passes in C
35. Rakudo Perl 6: What You Can Do Today
Problem
Get a random item from a list
Solution
my @drinks = <wine beer vodka>;
say "Tonight I'll drink { @drinks.pick }";
Output (results should vary ;-))
Tonight I'll drink vodka
36. Rakudo Perl 6: What You Can Do Today
Problem
Shuffle a list into a random order
Solution
my @competitors = <Tina Lena Owen Peter>;
my @order = @competitors.pick(*);
for @order { .say }
Output (results should vary ;-))
Peter
Lena
Owen
Tina
37. Rakudo Perl 6: What You Can Do Today
Problem
Write and call a subroutine with parameters
Solution
sub greet($greeting, $name) {
say "$greeting, $name!";
}
greet("hello", "masak");
Output
hello, masak
38. Rakudo Perl 6: What You Can Do Today
Problem
Write a subroutine that only takes a number
Solution
sub double(Num $n) { 2 * $n }
say double(21);
say double("oh no I'm not a number");
Output
42
Parameter type check failed; expected Num,
but got Str for $n in call to double
39. Rakudo Perl 6: What You Can Do Today
Problem
Use multi-subs to react differently by type
Solution
multi double(Num $n) { 2 * $n }
multi double(Str $s) { $s x 2 }
say double(21);
say double("boo");
Output
42
booboo
40. Rakudo Perl 6: What You Can Do Today
Problem
Compute factorial (recursively)
Solution
multi fact($n) { $n * fact($n - 1) }
multi fact(0) { 1 }
say fact(1);
say fact(10);
Output
1
3628800
41. Rakudo Perl 6: What You Can Do Today
Problem
Compute factorial (using a meta-operator)
Solution
sub fact($n) { [*] 1..$n }
say fact(1);
say fact(10);
Output
1
3628800
42. Rakudo Perl 6: What You Can Do Today
Problem
Add a new factorial operator (so 10! works)
Solution
sub postfix:<!>($n) { [*] 1..$n }
say 1!;
say 10!;
Output
1
3628800
43. Rakudo Perl 6: What You Can Do Today
Problem
Declare a class with attributes and a method
Solution
class Product {
has $.name; # Attr + accessor
has $!price; # Attr only
has $.discount is rw;
# Attr + lvalue accessor
method get_price {
return $!price - $!discount;
}
}
44. Rakudo Perl 6: What You Can Do Today
Problem
Instantiate a class and call a method on it
Solution
my $prod = Product.new(
name => "Beer",
price => 500,
discount => 60
);
say $prod.get_price;
Output
440
45. Rakudo Perl 6: What You Can Do Today
Problem
Get/set attributes through accessors
Solution
say $prod.name;
$prod.discount = 40;
say $prod.get_price;
$prod.name = 'Wine';
Output
Beer
460
Cannot assign to readonly variable.
46. Rakudo Perl 6: What You Can Do Today
Problem
Call a method on every object in a list
Solution
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 }
Output
BEER
WINE
VODKA
47. Rakudo Perl 6: What You Can Do Today
Problem
Introspect a class to find its methods
Solution
my @meths = Product.^methods(:local);
for @meths>>.name { .say }
Output
get_price
discount
name
48. Rakudo Perl 6: What You Can Do Today
Problem
Sort an array of objects by result of a method
Solution (Example 1)
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 }
Output (Example 1)
Beer
Vodka
Wine
49. Rakudo Perl 6: What You Can Do Today
Problem
Sort an array of objects by result of a method
Solution (Example 2)
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 }
Output (Example 2)
Wine
Beer
Vodka
50. Rakudo Perl 6: What You Can Do Today
Problem
Find minimum and maximum values from a list
Solution (Example 1)
my @temperatures = -3, 5, 7, 2, -1, -4, 0;
say "Minimum was " ~ @temperatures.min;
say "Maximum was " ~ @temperatures.max;
Output (Example 1)
Minimum was –4
Maximum was 7
51. Rakudo Perl 6: What You Can Do Today
Problem
Find minimum and maximum values from a list
Solution (Example 2)
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;
Output (Example 2)
Cheapest: Wine
Costliest: Vodka
52. Rakudo Perl 6: What You Can Do Today
Problem
Paper, Scissor, Stone game
Solution (Part 1)
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" }
53. Rakudo Perl 6: What You Can Do Today
Problem
Paper, Scissor, Stone game
Solution (Part 2)
say win(Paper, Paper);
say win(Scissor, Stone);
say win(Stone, Scissor);
Output
Draw
Lose
Win
55. Rakudo Perl 6: What You Can Do Today
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
56. Rakudo Perl 6: What You Can Do Today
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…)
57. Rakudo Perl 6: What You Can Do Today
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
58. Rakudo Perl 6: What You Can Do Today
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!)
59. Rakudo Perl 6: What You Can Do Today
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
64. Rakudo Perl 6: What You Can Do Today
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 ☺