Recently in Perl6 Category

On Saturday, I wrote up a possible API for Parrot compilers to support loading libraries written in other languages and discussed some of the details with Jonathan++ and Allison++. It’s not perfect, and is missing a few parts, but should be extensible enough to support whatever else we need in the future. I still need to formalize it a bit and add it to the Parrot docs and the example language shell.

On Sunday, I implemented it on Rakudo (Perl 6) and Cardinal (Ruby; very incomplete).

This morning, after confirming the spec with pmicahud++, I merged the changes into Rakudo trunk.

The syntax for specifying the source language for Perl 6 is:

use Foo:lang<cardinal>;

I couldn’t quite figure out what an appropriate way to do this in Ruby would be, so I just added a function to cardinal:

foreign_load('perl6','Foo/Bar')

If you have a better suggestion for what it should look like in Ruby, please let me know! I don’t actually know much Ruby at all, so my Ruby compiler is fairly limited.

I’ll be adding support for this to pynie (Python) soon, and other languages after that.

Here’s a simple example of using a Perl library from Ruby:

[sweeks@kweh ~]$ cat Foo.pm
module Foo {
    sub greet($name) is export {
        say "Hello, $name!"
    }
}
[sweeks@kweh ~]$ cat perl6.rb
foreign_load 'perl6', 'Foo'
['Ruby', 'Perl', 'World'].each { |name| greet name }
[sweeks@kweh ~]$ cardinal perl6.rb
Hello, Ruby!
Hello, Perl!
Hello, World!

Here’s a similar example of using a Ruby library from Perl:

[sweeks@kweh ~]$ cat Foo.rb
module Foo
    def greet(name)
        puts "hello, " + name
    end
    def apply_people(cb)
        people = ['Dave', 'Bryan', 'Stuart', 'Dax']
        people.each { |name| cb(name) }
    end
end
[sweeks@kweh ~]$ cat ruby.pl
use Foo:lang<cardinal>;
greet("person $_") for 1..5;
apply_people( { say "hello from perl, $^name" } )
[sweeks@kweh ~]$ perl6 ruby.pl
hello, person 1
hello, person 2
hello, person 3
hello, person 4
hello, person 5
hello from perl, Dave
hello from perl, Bryan
hello from perl, Stuart
hello from perl, Dax

Thanks go to my employer (Guru Labs) for their support in my work on Rakudo and Parrot.

Rakudo is just starting to get support for adding custom operators to the grammar from user-level code. You can’t specify the precedence yet, but you can run the traditional examples:

multi sub infix:<±>(Int $a, Int $b) { return $a + $b | $a - $b }
multi sub postfix:<!>(Int $a where { $_ > 0 }) { return [*] 1..$a }

my $x = 5! ± 2;
say "hi dood" if $x > 121;
say "hello again" if $x < 119;

I was playing around today with defining operators for mathematical set operations (∩ ∪ ∖ ⊂ ⊃ ⊆ ⊇ etc.) and then decided that I wanted a fancy syntax for defining sets, so I added support to rakudo for circumfix operator definition, and i now have this running on rakudo:

say "subset" if ⦃ 1, 3, 5 ⦄ ⊆ ⦃ 1, 2, 3, 4, 5 ⦄;

Blog speed-run.

| 175 Comments | No TrackBacks

On Sunday, before I left for my flight, I saw masak chatting on IRC about his blog speed-run, which I later found described here. He set a time limit to see how quickly he could implement a basic blog app on the tools we've been building.

At the airport later that day, I had 20 minutes to wait before boarding my flight, so I thought I'd try the same thing. Omgblog is what I got done before I needed to board. I'll try to leave it running for a while. The source is in the usual place.

I’ve still been working on the Web libraries for Perl 6.

In the past week, I’ve improved my Tags library to handle more common cases and be easier to use, I migrated HTTP::Daemon to use Parrot sockets instead of exec-ing socat, and I wrote a basic pastebin: http://github.com/masak/web/blob/c84c4c1892463459e406958db8f9232e7b7bf5d1/bin/kopipasta.pl.

You might be able to see a running instance here: http://pleasedieinafire.net:2080/. That host has poor connectivity lately, though.

I’ve also played a bit with some ideas related to dispatch. Here’s an example I got working on the plane home from Boston:

use LolDispatch;
use HTTP::Daemon;
sub item($request, $match) is handler(/^\/item\/(\d+)/) {
    say 'dispatched to item';
    say $match.perl;
}
my $request = HTTP::Request.new(
    req_url => HTTP::url.new(path => '/item/12345'),
    headers => HTTP::Headers.new( header_values => { 'Host' => 'localhost' }),
    req_method => 'GET',
);
dispatch($request);

Here’s the output of that example:

dispatched to item
Match.new(
 # WARNING: this is not working perl code
 # and for debugging purposes only
 ast  => "/item/12345",
 text => "/item/12345",
 from => 0,
 to   => 11,
 positional => [
  Match.new(
     ast  => "12345",
     text => "12345",
     from => 6,
     to   => 11,
    ),
 ],
)

I'm participating in the web framework for Perl 6 grant from The Perl Foundation. My first task has been working on a Tags library for (X)HTML generation.

I've got the start of a simple port of Template Declare implemented. It's not perfect, and I'm still working out the API we want, but it's functional, and nice. It's quite a bit nicer than CGI.pm-style html generation.

The philosophy of this library is that templates are code, and we should be able to deal with them as such. If your template is Perl, you can use all of the same tools you use for Perl. No need to learn a new language, and far fewer angle brackets. :)

Here's an example that runs under the current Tags.pm. Keep in mind, there's still a decent amount of API refactoring needed.

use Tags;
say show {
    html {
        head { title { 'Tags Demo' } }
        body {
            outs "hi";
            ul :id<numberlist> {
                outs "A list from one to ten:";
                for 1..10 {
                    li :class<number>, { $_ }
                }
            }
        }
    }
}

About this Archive

This page is an archive of recent entries in the Perl6 category.

Parrot is the previous category.

Find recent content on the main index or look in the archives to find all content.

Perl6: Monthly Archives

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en