Home > Perl6 > Why Perl 6 (part 1 (maybe))

Why Perl 6 (part 1 (maybe))

There seams to be much confusion what Perl 6 is for. In fact this article is
mostly in response to Die Woche: Endlich Weihnachten .

Larry told us in one of the State of the Onion talks that Perl 5 is actually
two languages. We have Perl 5 and PCRE in the same program and they can
happyly interact with each other.

There are many CPAN modules that extend Perl 5 with new language elements.
As a result it’s fairly easy to confuse syntax highlighting in any editor we
have. When it comes to autocompletion it’s all over because you can’t tell
what that thing under the curser is without running the script. And even
if you could it might change between any two calls of the same sub.

This certainly sucks.

The answer to that problem is an extensible grammar, capable of being handed
over to your trusty editor to deal with the problem without any human

Debugging and error messages are another field of confusion and grief in
dynamic languages. If you use a language with static typisation and you
know the type of something, you know what to expect. In a dynamic language
one can easyly be lost in a chain of indirection any greek hero would have
fled in terror.

Introducing types to Perl 6 helps and adding complete introspection solves
that problem. On top of that Larries STD.pm can not only parse valid Perl 6
code but unvalid code as well. Insted of getting uncomprehensable c++ error
messages, containing a template < in_a_template < in_a_template < in_a_template > > > the compiler takes care of you like this:

16:43 < masak> std: say $^a
16:43 <+p6eval> std 31896:
OUTPUT<<===SORRY!===Placeholder variable $^a may not be used outside of a block at /tmp/zu42FbdNcC line 1:NL------> say $^a?
Check failed
FAILED 00:01 115m

Getting back to the many language thingy I have a hypotetical example how
the future might look like.

use Javascript;


var div = document.gelElementById('content_container');
// do some more stuff in JS

use v6;

my @headlines = $div.children.grep: { *.tagName ~~ 'h1' | 'h2' | 'h3' };
# go on in perl6 or switch back to JS

Having more then one language in the same project is by no means alien to
anybody who is working with the web. But the languages we are using today
do not support us with that problem. Perl 6 provides introspection on any
level of the language. So we could go and use it for code generation
without parsing c++ header files or fragile Perl scripts trying to figure
out how to handle html forms the same way on server as on client side.

sub validateAddressForm(
  Str $email where { * ~~ /\w+@(\w+\.)+\w+/ }
 ,Str $zip where { * ~~ /\d{5}/ }


sub signature_to_html(Code $f){
	my Str $html = "
"; # I skip to check if there is any meaningfull signature here for &$f.signature.params -> $p { my $default = $p.default; my $name = $p.name; if $.type ~~ Str | Int { $html .= "<input name='$name' value='$default' />"; } } $html .= '
'; }

Just an example that works already. It’s not complete and it would be better
to generate a DOM tree to play with before it becomes HTML. But you should
clearly see where this is going to.

What you can’t see is that the where clause that is doing the range checks
is just an anonymous function that you can get hold of. The abstract syntax
tree is preserved and with the right module one could compile it to
javascript and use the very same form validation on client side as on server
side. All with a single function call.

Lines of codes you let a program write for you are lines you can’t add bugs

Rakudo is special here amongst the Perl Sixes because it is build on top of
Parrot. As any good talking bird, Parrot does not care what language it’s
ment to use. In fact you can teach it to talk more then one language in
the same script. Well, not right now but you will when the Parrot team got
that stuff working.

Working together with other programmers requires you to use the same
language in most cases. Parrot and Perl 6 are a step into a future where we
will be able to finally finish that tower of babel thingy and reach the sky.

Categories: Perl6
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: