I saw the latest parrot release notice on LWN this week and randomly
wondered the Perl6 native call interface was like compared to the
horrific mess that is Perl5 XS. The answer is that it is a thing of
beauty !
Here is the libguestfs hello world example in Perl6
$ dd if=/dev/zero of=guest.img bs=1M count=100
$ mke2fs guest.img
$ cat guestfs.p6
use NativeCall;
say "Defining stubs";
sub guestfs_create() returns OpaquePointer is native('libguestfs') {}
sub guestfs_add_drive(OpaquePointer $handle, Str $path) returns Int is
native('libguestfs') {}
sub guestfs_launch(OpaquePointer $handle) returns Int is native('libguestfs') {}
sub guestfs_mount(OpaquePointer $handle, Str $path, Str $target) returns Int is
native('libguestfs') {}
sub guestfs_touch(OpaquePointer $handle, Str $path) returns Int is
native('libguestfs') {}
sub guestfs_umount(OpaquePointer $handle, Str $path) returns Int is
native('libguestfs') {}
sub guestfs_shutdown(OpaquePointer $handle) returns Int is native('libguestfs')
{}
sub guestfs_close(OpaquePointer $handle) returns Int is native('libguestfs') {}
say "Creating handle";
my $g = guestfs_create();
say "Adding drive";
guestfs_add_drive($g, "guest.img");
say "Launching appliance";
guestfs_launch($g);
say "Mounting root";
guestfs_mount($g, "/dev/sda", "/");
say "Touching hello";
guestfs_touch($g, "/hello");
say "Unmounting root";
guestfs_umount($g, "/");
say "Shutting down appliance";
guestfs_shutdown($g);
say "Closing handle";
guestfs_close($g);
$ perl6 guestfs.p6
Defining stubs
Creating handle
Adding drive
Launching appliance
Mounting root
Touching hello
Unmounting root
Shutting down appliance
Closing handle
$ su -
# mount -o loop /home/berrange/guest.img /mnt/
# ls /mnt/
hello lost+found
Obviously for real world usage, we'd want to have the generator create
a module containing all the stub definitions. I've at least answered
my curiosity now :-)
If only someone would port the Perl6 'NativeCall' module to Perl5, we
could kill of the XS horrificness there too
Regards,
Daniel
--
|:
http://berrange.com -o-
http://www.flickr.com/photos/dberrange/ :|
|:
http://libvirt.org -o-
http://virt-manager.org :|
|:
http://autobuild.org -o-
http://search.cpan.org/~danberr/ :|
|:
http://entangle-photo.org -o-
http://live.gnome.org/gtk-vnc :|