I have a Perl script similar to
my $relSourcePath; my $inlineCFile; my $inlineBuildDir; my $libDir; my $incDir; BEGIN { # this file is in the same directory as the rest of the sources $relSourcePath = File::Spec->rel2abs(dirname($0)); die "Install path must not have spaces.\n" if $relSourcePath =~ /\ +s/; # Need to build the path to the Inline C file here, so that we cou +ld # reference it below my @dirs = (); push @dirs, $relSourcePath; $inlineCFile = File::Spec->catfile(@dirs, "my_wrapper.c"); push @dirs, ".Inline"; $inlineBuildDir = File::Spec->catdir(@dirs); pop @dirs; push @dirs, "lib"; $libDir = File::Spec->catdir(@dirs); pop @dirs; push @dirs, "include"; $incDir = File::Spec->catdir(@dirs); } use Inline ( C => Config => LIBS => "-L$libDir -lmylib", INC => "-I$incDir", DIRECTORY => $inlineBuildDir, ); # this is the C source file for interfacing to the library. Yes, this +could be # done natively in Perl, but this is just as easy (and probably faster + to # execute). use Inline C => "$inlineCFile"; $ENV{MYVAR} = "my_var_value"; print "ENV{MYVAR} = '" . qqq("MYVAR") . "'\n";
The my_wrapper.c file is basically a wrapper to glue together mylib with Perl code. As a test, I added a function qqq to my_wrapper.c as follows:
char * qqq(const char *envName) { return getenv(envName); }
When I run the Perl script, I see that Perl does indeed set the environment variable so that the qqq() function can extract its value. However, it appears that the value of the environment variable is not the same when executing code within mylib. It's as though, mylib was loaded into a different memory space

Is there anything I can do in Perl to set the environment variable so that it's visible to mylib?

P.S. As a sanity test, I set the environment variable on the command line first and then ran my test. The value used by mylib is the one that was set on the command line and not the one from within Perl.


In reply to Inline::C and environment vars by gri6507

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.