Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

reseting PATH

by Anonymous Monk
on Apr 11, 2011 at 13:16 UTC ( [id://898699]=perlquestion: print w/replies, xml ) Need Help??

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

I have a problem in pre-pending to PATH for a captive script. I'm trying to append the location of the perl executable I wish to use to the beginning of the PATH and then exec it to pick up the change. Since this perl script is called directly in a captive environment I am unable to set these environment variables in calling script first. This method works fine for other environment variables (I picked this code up from a response to another question by Tye) but not for the PATH.
# Set up the perl executable and library path BEGIN { $ENV{PATH} = "/mypath/perl/bin:$ENV{PATH}"; $ENV{PERL5LIB} = '/mypath/perl/lib/site_perl/5.8.8/sun4-solari +s:/mypath/perl/lib/site_perl/5.8.8:/mypath/perl/lib/site_perl:/mypath +/perl/lib/5.8.8:/mypath/perl/lib/site_perl'; exec 'env', $^X, $0, @ARGV; }

The problem is that when I do the above the executable never returns and I cannot figure out what it is doing. I suspect it is constantly re executing itself in an infinite loop
Any help appreciated

Replies are listed 'Best First'.
Re: reseting PATH
by Corion (Patriarch) on Apr 11, 2011 at 13:20 UTC

    How will your Perl program know that it has already re-executed itself?

    My approach would be to set a second environment variable to tell your program that it is already re-executing:

    warn "$0 launched"; if (! $ENV{RE_EXECUTING}) { $ENV{RE_EXECUTING} = 1; warn "Set up environment. Re-executing now."; exec ... };

    But maybe you want to just check whether the environment needs changing at all, and only if you need changes re-execute?

      Thanks. I knew the answer was staring me in the face.
Re: reseting PATH
by ikegami (Patriarch) on Apr 11, 2011 at 16:22 UTC

    I think it's safer to pass an argument.

    Secondly, env $^X makes no sense. You want one of

    • exec $^X, ...
    • exec 'env', 'perl', ...
    • exec '/mypath/perl/bin/perl', ...

    Your other changes would indicate you want the last.

    Thirdly, the changes to PERL5LIB look redundant with the paths built into the Perl you want to be running.

    BEGIN { if (@ARGV && $ARGV[0] eq '--fixed') { shift(@ARGV); } else { $ENV{PATH} = join ':', '/mypath/perl/bin', $ENV{PATH}; delete $ENV{PERL5LIB}; exec '/mypath/perl/bin/perl', $0, '--fixed', @ARGV; die ...; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://898699]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-23 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found