robhoelz has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Perl fans, I'm currently working on a Lua-Perl bridge, and I'm implementing a function perl.require that loads a Perl module from Lua, in the same manner as CORE::require. The usage is as follows:
require 'perl' perl.require 'Scalar::Util'
This works fine, but if I'm missing a module...
require 'perl' perl.require 'Doesnt::Exist::Yet'
In this case, Perl dies with a module not found error and calls exit(). Here's my implementation of perl.require:
static int perl_interp_require(lua_State *L) { size_t len; const char *modname; SV *modnameSv; modname = luaL_checklstring(L, 1, &len); modnameSv = newSVpvn(modname, len); /* Exits on failure */ load_module(PERL_LOADMOD_NOIMPORT, modnameSv, NULL); check_perl_error(L, 1); // left out for brevity; checks $@ return 0; }
Is there a way to catch any errors that load_module may throw? I did a quick search for "perl xs load_module catch die", but it yielded nothing useful. Thanks for any and all help! -Rob

Replies are listed 'Best First'.
Re: Catching a die in Perl XS
by ikegami (Pope) on Jun 02, 2010 at 19:44 UTC
    SV* rv = eval_pv("require Module; import Module; 1", 0); if (!SvTRUE(rv)) { ... handle exception ... }

    $@ is available as ERRSV.