Categories
Posts in this category
- A shiny perl6.org site
- Creating an entry point for newcomers
- Sprixel, a 6 compiler powered by JavaScript
- Another perl6.org iteration
- Blackjack and Perl 6
- Why I commit Crud to the Perl 6 Test Suite
- Report from the Perl 6 Hackathon in Copenhagen
- Custom operators in Rakudo
- Defined Behaviour with Undefined Values
- Dissecting the "Starry obfu"
- Perl 6: Failing Softly with Unthrown Exceptions
- The first Perl 6 module on CPAN
- Google Summer of Code Mentor Recap
- Building a Huffman Tree With Rakudo
- Immutable Sigils and Context
- Is Perl 6 really Perl?
- Perl 6: Lost in Wonderland
- Lots of momentum in the Perl 6 community
- Musing and the future of feather and the Pugs repository
- Musings on Rakudo's spectest chart
- My first executable from Perl 6
- Trying to implement new operators - failed
- Let's build an object
- Perl 6 is optimized for fun
- How to get a parse tree for a Perl 6 Program
- Perl 6 in 2009
- Perl 6 ticket life cycle
- The Perl 6 Advent Calendar
- How to Plot a Segment of a Circle with SVG
- Publicity for Perl 6
- Rakudo architectural overview
- Rakudo Rocks
- Rakudo "star" announced
- Rakudo's rough edges
- Rats and other pets
- Releasing Rakudo made easy
- Set Phasers to Stun!
- Starry Perl 6 obfu
- Recent Perl 6 Developments August 2008
- Strings and Buffers
- Subroutines vs. Methods - Differences and Commonalities
- A SVG plotting adventure
- A Syntax Highlighter for Perl 6
- Test Suite Reorganization: How to move tests
- The Happiness of Design Convergence
- Perl 6 Tidings from September and October 2008
- Perl 6 Tidings for November 2008
- Perl 6 Tidings from December 2008
- Perl 6 Tidings from January 2009
- Perl 6 Tidings from February 2009
- Perl 6 Tidings from March 2009
- Perl 6 Tidings from April 2009
- Perl 6 Tidings from May 2009
- Perl 6 Tidings from May 2009 (second iteration)
- Perl 6 Tidings from June 2009
- Perl 6 Tidings from August 2009
- Perl 6 Tidings from October 2009
- Timeline for a syntax change in Perl 6
- Visualizing match trees
- We write a Perl 6 book for you
- When we reach 100% we did something wrong
- Where Rakudo Lives Now
- Why was the Perl 6 Advent Calendar such a Success?
- What you can write in Perl 6 today
- Why you don't need the Y combinator in Perl 6
Fri, 24 Apr 2009
What you can write in Perl 6 today
Permanent link
You've probably heard of Perl 6 before; this weird new version of Perl that Larry Wall is obsessed with, and that's being worked on since the year 2000, and for which there's still no real compiler. Right?
Well, nearly. In 2000 the decision for a complete rewrite of the language was made, hacking on Parrot (a virtual machine intended to run Perl 6 and other dynamic languages) began 2001, but hacking on Rakudo, the Perl 6 compiler on top of Parrot, began in earnest in December 2007.
These days Rakudo is, in my humble opinion, fairly usable. It's not fast, and not feature complete, but it implements many neat features, and passes more than 10,000 tests today.
I've picked a few example of what Rakudo can run today, in the hope that it will impress some Perl 5 hackers and generate more interest in Perl 6 and Rakudo.
Built-in Schwartzian Transformation
This one is not overly complicated, but very handy anyway. If you want to sort an array case insensitively, in Perl 5 you'd write
# (Perl 5) @a = sort { uc($a) cmp uc($b) } @a; # or if you don't want to duplicate your uc effor, you do a ST: @a = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, uc] } @a;
(See Schwartzian Transform for details on what this does).
Perl 6's sort built-in is smart enough to recognize a block that
expects only one parameter, and automatically does the ST for you:
# (Perl 6) @a = @a.sort: { .uc };
Built-in functions don't default to $_ anymore, but a method call
without invocant does, so the .uc is equivalent to
$_.uc.
If you want to golf, you can write that even shorter: @a = @a.sort:
*.uc. The *.method syntax automatically writes the closure for
you.
Sorting Hashes
Another useful sort technique is Hash.sort. It passes Pairs
(ie single key-to-value mappings) to its comparison routine, making the
to hash items unnecessary that you might know from Perl 5 hash sorting
code.
Sorting a Hash by value, for example, is as easy as
# (Perl 6) my %leaders = USA => 'Obama', Germany => 'Merkel', France => 'Sarkozy'; say %leaders.sort(*.value).perl; # Output: # ["Germany" => "Merkel", "USA" => "Obama", "France" => "Sarkozy"]
Iterating made easy
Iterating arrays and hashes was never hard in Perl, but in Perl 6 a few
things are even simpler than before. Suppose you want to iterate an array, but
obtain two items in each iteration. In Perl 5 you'd either write a C-style
for loop iterating over the index in steps of two, or use
List::MoreUtils::natatime (if you can spell it ;-).
But this is so much easier:
for <a b c d> -> $x, $y { say "$x | $y"; } # output: # a | b # c | d
This also helps when you want to iterate over both index and values:
for <a b c>.kv -> $k, $v { say "key: $k value: $v"; } # output: # key: 0 value: a # key: 1 value: b # key: 2 value: c
The same code also works with Hashes.
Reductions
Long known to users of functional programming languages, reductions can be
very easy in Perl 6. Instead of 1 + 2 + 3 you can write
[+] 1, 2, 3, where [...] is the reduction meta
operator.
Some examples:
say [+] 1..5; # sum say [*] 1..5; # product say [<] @list; # is @list strictly ordered ascendingly? say [!=] @list; # are no two consecutive items equal? say [eq] @list; # are all items string-equal?
Parametric Roles
Perl 6 comes with a fully fledged object system including roles (pieces of behavior and data that can be composed into classes and objects).
Not only that, but you can also give the roles parameters, which are bound to values at composition time.
use v6; role Tax[Num $rate] { method pay-tax { my $tax = $.income * $rate; $!income -= $tax; say "Paying $tax Dollars tax"; } } class Banker { has $.income = 234000; } my $bailout = Banker.new() but Tax[0.90]; $bailout.pay-tax(); # output: Paying 210600 Dollars tax
(To get something similar in Perl 5 you'd need Mooseand MooseMooseX::Role::Parameterized]).
Parametric roles are used internally to implement typed arrays and hashes.
my Int @a = 1, 2, 3; @a[1] = 4; # fine @a[2] = 'foo'; # Error: Type mismatch in assignment.
The my Int @a declares an Array that's parameterized by the type of
array items, here Int.
Conclusions
These are just a few very basic examples of cool things to do with Perl 6 today. Even more exciting are for example the regexes and grammars, about which I won't talk now because then I wouldn't finish before the end of the month ;-)
If you haven't tried Perl 6, now is a good time. Download and build it now, and
if you have any questions go to #perl6 on irc.freenode.net, or ask on
perl6-users@perl.org.
Comments / Trackbacks:
Trackback URL:
/blog-en/perl-6/writing-perl6-now.trackback
Robert Bond wrote
Great blog
Hi Moritz, I enjoy your lucid writing style!
wolverian wrote
Great post for Perl 5 programmers. I do not like the site colours though. :)
Pedro Melo wrote
$!income?
Hi,
What does this do:
$!income -= $tax;
I was expecting $.income -= $tax;...
Thanks in advance,
Moritz wrote
$!income vs. $.income
That requires a bit more explanation.
Attributes always have the ! sigil, so the attribute is named $!income. The declaration 'has $.income' is a shortcut for declaring both the attribute $!income and the (read-only) accessor Banker.income, which is just an ordinary public method.
So inside the class you usually use the !-Form, because the one with the dot is read-only.
Chris Dolan wrote
$_
You say that "{ .uc }" defaults to working on $_. But really, it defaults to working on the topic that's passed in, not on $_, right? So:
my $method = { .say };
$_ = "foo";
"bar".$method();
would yield "bar", not "foo", right?
Chris wrote
Well-written
That was a very well written piece.
Moritz wrote
$_ explanations
Actually both is true ;-)
{ .uc } constructs a closure with one optional parameter. If an argument is provided, $_ is bound to it. If not, the caller's $_ is used.
Clayton Scott wrote
Difference in array vs hash sorting?
Why is this syntax for sorting arrays:
@a.sort: { .uc };
So much different from this syntax for sorting hashes?
%leaders.sort(*.value);
Is it another instance of TIMTOWTDI?
Moritz wrote
Hash vs. Array
Yes, it's timtowtdi, actually two instances of it.
The first one is that
$obj.method: $arguments
and
$obj.method($aruments)
both mean the same thing (but the latter is more practical when you want to chain method calls).
The second is that
*.foo
and
{ $_.foo }
both construct a closure that calls the method 'foo' on its first argument.
Brian Wisti wrote
Lots of interesting stuff! The reductions don't quite work as advertised, though. When I use the ranges for [+] and [*] I get somewhat mysterious boolean-type responses:
say [+] 1...5; # sum -> displays '0'
say [*] 1...5; # product -> displays '1'
When I explicitly spell out what we need the sum and product of, it works better:
say [+] 1, 2, 3, 4, 5; # sum -> displays '15'
say [*] 1, 2, 3, 4, 5; # product -> displays '120'
This is with the version of Rakudo in git on 2009-06-15. The notes do say Rakudo moves fast, so maybe the rules changed since you wrote this post.
Moritz wrote
Ranges
The problem is that we use three dots to construct a range, instead of two. I have no idea how Rakudo parses that, but certainly not as a range.
I'll update the post to correct it, it should be
say [+] 1..5;
Write a comment
The comments on this blog post have been disabled; the comment form below will not work.