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

Dear monks, my question is as short as title:
How can I add directory to @INC from C code, when I'm embeding perl?

A little bit longer explanation. I have embeded perl in my c application, but I have problem, that even without xs_init() like here embedding perl functions with 'use()' I get error: Can't locate module-name.pm in @INC ... (@INC contains .) I understand, that it is because @INC is not iniialized automatically, so it is empty.

Replies are listed 'Best First'.
Re: How @INC can be managed from c code?
by Corion (Patriarch) on Apr 01, 2015 at 13:26 UTC

    I think that @INC is just another Perl variable. You could either run Perl code to populate @INC:

    push @INC, '/opt/myapp/lib';

    ... or you could do the same in C:

    AV * PL_inc = get_av("INC",0); if( ! PL_inc ) { Perl_croak(aTHX_ "@INC not found from C code?!"); }; av_push(results, newSVpv("/opt/myapp/lib"));
      Have you meant
      av_push(PL_inc, newSVpv("/opt/myapp/lib", 15));
      However, it can only be called after perl_parse() So maybe I'm on wrong way.

        Yeah, the results was a leftover from pasting together the code. I don't really do C, and don't interact much from C-space with the Perl interpreter, but I think you can call perl_parse() multiple times, just like -e does.

Re: How @INC can be managed from c code? ( runperl.c )
by Anonymous Monk on Apr 01, 2015 at 18:12 UTC
      Thank you! Sorry, I cannot check this right now, but as soon as I can, I'll reply!
Re: How @INC can be managed from c code?
by LanX (Saint) on Apr 01, 2015 at 13:49 UTC
    Not sure at all, but setting environment might help.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

      I'm building application to deploy on other machines, it's better not to rely on environment variables.
        An application can only change it's own environment and passes it to it's child processes.

        But I can't tell if embedded Perl code still evaluates %ENV.

        May be only a question of initialization phase and timing, setting it as early as possible might help.

        Better just try it out and tell us.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        PS: Je suis Charlie!

        update

        have a look at PERLLIB