lunes, 20 de abril de 2009

My first perl6 dabblings

It's been a month since parrot 1.0 and now , rakudo is passing more than 3000 tests than then. It's an amazing progress IMHO.

I just started to write very simple scripts to get the feeling of perl6. This is my first one.

A script that gets 2 integers per row, and then prints a honeycomb of the given size (rows x cols)

it's a really simple script, but at least it gives little hints of how functions are called, strings printed, and STDIO read.


#!/tmp/rakudo/perl6

sub printa_rusc($f, $c){

my $first = ' - ' x $c ;
$first.chop.say; #chop is non destructive

my $top_line = '/ \_' x $c;
my $bottom_line = '\_/ ' x $c;

for 1..$f {
$top_line.chop.say;
$bottom_line.chop.say;
}
}

while my $tat = =$*IN {
my @arr=$tat.split(/\s+/);
@arr.perl.say;
if 2==@arr.elems {
printa_rusc(@arr[0], @arr[1]); #not $arr[0]
} else {
"Bad format. Enter only 2 integers".say;
}
}

# vim: set tabstop=4 shiftwidth=4 foldmethod=marker ft=perl: ##

We can see some strange things if we're used to perl5.

First the @arr[0] thing. In perl6, sigils are part of the symbol, so the @rray @arr will always be acessed as @arr[1], @arr[1..10] ,... it happens the same with %ashes .

chop is not destructive, meaning that returns a copy of what was choped, but it doesn't modify the given argument.

We see something like @arr.perl.say . As you probably know, .say is the method to print things followed by a "\n". print "hi", "\n" ; is the same as say "hi"; or "hi".say; .

For the moment there's not much more to say.

See the rakudo's progress at http://www.rakudo.org/status . today 23/04/09

To get rakudo, it's pretty easy: (from rakudo.org site)

$ git clone git://github.com/rakudo/rakudo.git

If you don't have git installed, you can get a tarball or zip of Rakudo from github by visiting http://github.com/rakudo/rakudo/tree/master and clicking "Download". Then unpack the tarball or zip.

Once you have a copy of Rakudo, build it as follows:

$ cd rakudo
$ perl Configure.pl --gen-parrot
$ make



That's all!

Thanks to parrot and rakudo devs for this new version of our favourite language

No hay comentarios: