This module allows you to embed a Perl interpreter within LuaJIT using the FFI library. It provides a simple API to initialize a Perl interpreter and execute Perl code.
- Ensure you have
perl.luaandperl_ffi.soin the same directory. - Place this directory in your Lua package path, or ensure the files are in a location where
require("perl")can find them. - The
perl_ffi.solibrary is a shared library compiled from the Perl source code. It is configured to be relocatable, so it should work as long as it is in the same directory asperl.lua.
local perl = require("perl")
-- Create a new Perl interpreter instance
local p, err = perl.new()
if not p then
error("Failed to create Perl interpreter: " .. err)
end
-- Run Perl code
-- The arguments simulate the command line arguments passed to the perl executable
local args = {"perl", "-e", "print 'Hello from Perl!\\n'"}
local result, err = p:run(args)
if result ~= 0 then
error("Perl execution failed: " .. (err or "unknown error"))
endCreates a new Perl interpreter instance.
- Returns:
p: A table representing the Perl interpreter instance, ornilon failure.err: An error message string if creation failed.
Parses and executes Perl code using the interpreter instance.
- Parameters:
args: A table of strings representing the command-line arguments to pass to the Perl interpreter. The first argument should typically be "perl".
- Returns:
result: The exit code of the Perl execution (0 usually indicates success).err: An error message string if theperl_parsestep failed.
To build the perl_ffi.so library, you need the Perl source code.
- Clone the Perl repository.
- Configure the build with
-Duseshrplibto generate a shared library. - Compile the source.
- Rename the generated
libperl.dylib(on macOS) toperl_ffi.so. - Use
install_name_toolto set the install name to@loader_path/perl_ffi.sofor portability.