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.
