RuPerl - Rust with embedded Perl
Thanks to a colleague who introduced me to Nim during last week’s SUSE Labs conference, I became a man with a dream, and after fiddling with compiler flags and obviously not reading documentation, I finally made it.
This is something that shouldn’t exist; from the list of ideas that should never have happened.
But it does. It’s a Perl interpreter embedded in Rust. Get over it.
Once cloned, you can run the following commands to see it in action:
cargo run --verbose -- hello.pm showtime
cargo run --verbose -- hello.pm get_quick_headers
How it works
There is a lot of autogenerated code, mainly for two things:
bindings.rs
andwrapper.h
; I made a lot of assumptions andperlxsi.c
may or may not be necessary in the future (seemain::xs_init_rust
), depends on how bad or terrible myC
knowledge is by the time you’re reading this.xs_init_rust
function is the one that does the magic, as far as my understanding goes, by hooking upboot_DynaLoader
to DynaLoader in Perl via ffi.
With those two bits in place, and thanks to the magic of the bindgen
crate, and after some initialization, I decided to use Perl_call_argv
, do note that Perl_
in this case comes from bindgen, I might change later the convention to ruperl
or something to avoid confusion between that a and perl_parse
or perl_alloc
which (if I understand correctly) are exposed directly by the ffi interface.
What I ended up doing, is passing the same list of arguments (for now, or at least for this PoC), directly to Perl_call_argv
, which will in turn, take the third argument and pass it verbatim as the call_argv
Perl_call_argv(myperl, perl_sub, flags_ptr, perl_parse_args.as_mut_ptr());
Right now hello.pm defines two sub routines, one to open a file, write something and print the time to stdout, and a second one that will query my blog, and show the headers. This is only example code, but enough to demostrate that the DynaLoader works, and that the embedding also works :)
I got most of this working by following the perlembed guide.
Why?
Why not?.
I want to see if I can embed also python in the same binary, so I can call native perl, from native python and see how I can fiddle all that into os-autoinst
Where to find the code?
On github: https://github.com/foursixnine/ruperl or under https://crates.io/crates/ruperl