moderated >> embedded perl $ENV{PATH} cannot be changed in instance other than the first

by rskartick » Thu, 23 Sep 2004 06:10:10 GMT

I have embedded perl in my c program, however I have encountered a
problem where the $ENV{PATH} can only be updated in the first
PerlInterpreter instance.
I get "unable to start foo.sh: No such file or directory at -e line
2." It appears perl is not using the updated PATH.

I am using perl 5.8.0, compile-time options: DEBUGGING MULTIPLICITY
USE_ITHREADS USE_LARGE_FILES PERL_IMPLICIT_CONTEXT on Red Hat
Enterprise Linux WS release 3 (Taroon Update 1).

The following is from the docs with modifications to show the problem:

#include <EXTERN.h>
#include <perl.h>

#define SAY_HELLOA "-e", "print qq(Hi, A I'm $^X\n)"
#define SAY_HELLOB "-e", "print qq(Hi, B I'm $^X\n); $ENV{PATH} .=
\":/home/username/foo\"; open(FOO, \"foo.sh |\") or die \"unable to
start foo.sh: $!\"; close FOO;"
int main(int argc, char **argv, char **env)
{
PerlInterpreter
*one_perl = perl_alloc(),
*two_perl = perl_alloc();
char *one_args[] = { "one_perl", SAY_HELLOA };
char *two_args[] = { "two_perl", SAY_HELLOB};
PERL_SET_CONTEXT(one_perl);
perl_construct(one_perl);
PERL_SET_CONTEXT(two_perl);
perl_construct(two_perl);
PERL_SET_CONTEXT(one_perl);
perl_parse(one_perl, NULL, 3, one_args, (char **)NULL);
PERL_SET_CONTEXT(two_perl);
perl_parse(two_perl, NULL, 3, two_args, (char **)NULL);
PERL_SET_CONTEXT(one_perl);
perl_run(one_perl);
PERL_SET_CONTEXT(two_perl);
perl_run(two_perl);
PERL_SET_CONTEXT(one_perl);
perl_destruct(one_perl);
PERL_SET_CONTEXT(two_perl);
perl_destruct(two_perl);
PERL_SET_CONTEXT(one_perl);
perl_free(one_perl);
PERL_SET_CONTEXT(two_perl);
perl_free(two_perl);
}


This program will fail, yes foo.sh exists and it is not in my initial
path. However, if you run SAY_HELLOB in the the first interpreter
everything will work!

char *one_args[] = { "one_perl", SAY_HELLOB };
char *two_args[] = { "two_perl", SAY_HELLOA};

Any ideas, suggestions. I have tried this on win32 and it works fine.

thanks ... sherwin

Similar Threads

1. embedded perl path $ENV{PATH} problem

I have embedded perl in my c program, however I have encountered a
problem where the $ENV{PATH} can only be updated in the first
PerlInterpreter instance.
I get "unable to start foo.sh: No such file or directory at -e line
2."  It appears perl is not using the updated PATH.

I am using perl 5.8.0, compile-time options: DEBUGGING MULTIPLICITY
USE_ITHREADS USE_LARGE_FILES PERL_IMPLICIT_CONTEXT on Red Hat
Enterprise Linux WS release 3 (Taroon Update 1).

The following is from the docs with modifications to show the problem:

 #include <EXTERN.h>
 #include <perl.h>

 #define SAY_HELLOA "-e", "print qq(Hi, A I'm $^X\n)"
 #define SAY_HELLOB "-e", "print qq(Hi, B I'm $^X\n); $ENV{PATH} .=
\":/home/username/foo\"; open(FOO, \"foo.sh |\") or die \"unable to
start foo.sh: $!\"; close FOO;"
 int main(int argc, char **argv, char **env)
 {
     PerlInterpreter
         *one_perl = perl_alloc(),
         *two_perl = perl_alloc();
     char *one_args[] = { "one_perl", SAY_HELLOA };
     char *two_args[] = { "two_perl", SAY_HELLOB};
     PERL_SET_CONTEXT(one_perl);
     perl_construct(one_perl);
     PERL_SET_CONTEXT(two_perl);
     perl_construct(two_perl);
     PERL_SET_CONTEXT(one_perl);
     perl_parse(one_perl, NULL, 3, one_args, (char **)NULL);
     PERL_SET_CONTEXT(two_perl);
     perl_parse(two_perl, NULL, 3, two_args, (char **)NULL);
     PERL_SET_CONTEXT(one_perl);
     perl_run(one_perl);
     PERL_SET_CONTEXT(two_perl);
     perl_run(two_perl);
     PERL_SET_CONTEXT(one_perl);
     perl_destruct(one_perl);
     PERL_SET_CONTEXT(two_perl);
     perl_destruct(two_perl);
     PERL_SET_CONTEXT(one_perl);
     perl_free(one_perl);
     PERL_SET_CONTEXT(two_perl);
     perl_free(two_perl);
 }


This program will fail, yes foo.sh exists and it is not in my initial
path.  However, if you run SAY_HELLOB in the the first interpreter
everything will work!

char *one_args[] = { "one_perl", SAY_HELLOB };
char *two_args[] = { "two_perl", SAY_HELLOA};

Any ideas, suggestions.  I have tried this on win32 and it works fine.

thanks ... sherwin

2. DBI connects to one oracle instance but cannot connect to others - Perl DBI

3. mod_perl ignoring changed to $ENV{{PATH}?

I've narrowed down to a simple script a problem where it looks like
when running under mod_perl, it does not support changes to the
$ENV{PATH} variable - it does require the assignment to avoid the
tainting, but then any assignment itself does not take effect - I
added /usr/local/bin to PATH, but am unable to execute commands in
that folder when running under mod_perl.

Could not find anything about this in the mod_perl web pages, but a
Usenet search seems to suggest that mod_perl only honors PerlSetEnv
PATH in config, and does not honor PATH changes in the script? That
does not sound right, what about the cases where a PATH change is
needed for some scripts only, so a global PerlSetEnv would be too
much.

Example - for testing, I copied /bin/echo to /usr/local/bin/echo, and
then ran this script - it runs fine when run under the shell, but when
run under Apache + mod_perl, it fails.

Script:
#!/usr/bin/perl -Tw

print "Content-type: text/plain\n\n";

$ENV{PATH} = "/bin:/usr/bin:/usr/local/bin:";
foreach ( "echo", "myecho", "/usr/local/bin/myecho") {
    print "------ Testing command '$_'\n";
    my $string = `$_ testing execution of '$_'`;
    print "    failed to execute '$_', \$? is $?/" . ($? >> 8) . " : $!
\n"
        if ($? != 0);
    print "    \$string is: $string\n";
}

Shell output (perl -Tw script)
Content-type: text/plain

------ Testing command 'echo'
    $string is: testing execution of echo

------ Testing command 'myecho'
    $string is: testing execution of myecho

------ Testing command '/usr/local/bin/myecho'
    $string is: testing execution of /usr/local/bin/myecho

Web page output - this fails to execute myecho without the path:
------ Testing command 'echo'
    $string is: testing execution of echo

------ Testing command 'myecho'
    failed to execute 'myecho', $? is 32512/127 :
    $string is:
------ Testing command '/usr/local/bin/myecho'
    $string is: testing execution of /usr/local/bin/myecho

4. ways to change the first char from a filename (with full path) - Perl

5. ways to change the first char from a filename (with full path )

6. Perl one liner to search for first instance within input - Perl

7. [Windows embedding] sys.path not properly initialized (was: PyImport_ImportModule/embedding: surprising behaviors)

8. Replace the first instance only of a string - Perl