Categories

Posts in this category

Tue, 05 May 2009

My first executable from Perl 6


Permanent link

In various occasions I have been asked if "Perl 6 compiles programs into real executables" or so.

The answer so far has always been the same: Perl 6 is a language specification, and it's up to the implementations if they offer that option, it's not a required feature.

But today I actually compiled a Perl 6 program into an executable, with Rakudo. It's not automated, so it takes a few steps, but it would be easy to wrap into a shell script or Makefile. Here it goes:

$ cat hello.pl
say "Hello, Perl people";
$ ./perl6 --target=PIR hello.pl > hello.pir
$ ./parrot/parrot -o hello.pbc hello.pir
$ ./parrot/pbc_to_exe hello.pbc > hello
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV),
dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped
$./hello
Hello, Perl people

This is what the Parrot folks call a "fake executable" - it contains the byte code as a string, links to libparrot, and has a small main program that initializes parrot. But hey, it's an executable ;-)

[/perl-6] Permanent link

Comments / Trackbacks:

Trackback URL: /blog-en/perl-6/my-first-executable.trackback

j1n3l0 wrote


Cool!

I managed to do the same on a Mac with an interactive perl6 program:

mib19412i:~/Applications/rakudo io1$ cat > hello.pl
use v6;
sub MAIN() {
my $name = prompt 'What is our name: ';
say "Hello, {$name}!";
}
mib19412i:~/Applications/rakudo io1$ ./perl6 --target=PIR hello.pl > hello.pir
mib19412i:~/Applications/rakudo io1$ ./parrot/parrot -o hello.pbc hello.pir
mib19412i:~/Applications/rakudo io1$ ./parrot/pbc_to_exe hello.pbc > hello
mib19412i:~/Applications/rakudo io1$ file hello
hello: Mach-O executable i386
mib19412i:~/Applications/rakudo io1$ ./hello
What is our name: Nelo
Hello, Nelo!
mib19412i:~/Applications/rakudo io1$

A couple of notes/issues to raise:

1 - This has to be done in the same directory as the perl6 executable (I think the rakudo folk are already aware of this)
2 - There is an --output option for the perl6 executable but it does not output to the specified file

Moritz wrote

output option
The --output option works here (thanks; I forgot about that. Actually it's preferable over a redirect because an aborted compilation won't clobber the output file with --output), you just have to use an = sign:

./perl6 --target=PIR --output=bar Test.pm

Klaus wrote

Or slightly less bloated ;-)
<code>
$ ls -l hello
-rwxr-xr-x 1 klaus klaus 440 7. Mai 13:42 hello
$ ldd hello
not a dynamic executable
$ file hello
hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, stripped
$ ./hello
Hello, Perl people
</code>

Ahmad Zawawi wrote


It also works on win32 (moritz++):

$ perl6 --target=PIR hello.pl > hello.pir
$ parrot\parrot -o hello.pbc hello.pir
$ parrot\pbc_to_exe hello.pbc
$ dir hello.exe
05/14/2009 04:33 PM 505,654 hello.exe
$ hello.exe

mark wrote


can you do static programs as well?

this would be kinda cool

Moritz wrote

Broken
Currently the fake-executable generation is broken, see http://rt.perl.org/rt3/Public/Bug/Display.html?id=65994

Michael wrote

Didn't work for me
I'm using Rakudo Pittsburgh on Lnux 64bit and this is what I got:
[root@mrPeanut rakudo-2009-06]# ./perl6 --target=PIR circle2.pl > circle2.pir
[root@mrPeanut rakudo-2009-06]# ./parrot/parrot -o circle2.pbc circle2.pir
[root@mrPeanut rakudo-2009-06]# ./parrot/pbc_to_exe circle2.pbc > circle2
[root@mrPeanut rakudo-2009-06]# ./circle2
Class '[ 'parrot' ; 'Perl6MultiSub' ]' not found
current instr.: 'perl6;Perl6Role;!add_variant' pc 3875 (src/classes/Role.pir:42)
called from Sub '!create_simple_role' pc 20874 (src/builtins/guts.pir:1304)
called from Sub '' pc 4260 (src/classes/Abstraction.pir:15)
called from Sub '' pc 35 (circle2.pir:25)
called from Sub '_block11' pc -1 ((unknown file):-1)
[root@mrPeanut rakudo-2009-06]#

Write a comment

The comments on this blog post have been disabled; the comment form below will not work.

 
Name:
URL: [http://www.example.com/] (optional)
Title: (optional)
Comments:
Save my Name and URL/Email for next time