Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Modifying ENV variable of parent process

by Tuna (Friar)
on Jun 03, 2001 at 01:51 UTC ( [id://85229]=perlquestion: print w/replies, xml ) Need Help??

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

My question has been discussed here setting enviornment variables for parent process, but I'm still unclear as to the answer.

Over coffee this morning, a DBA friend of mine asked my advice about a problem she is having. She has a program called "oraenv", that among other things, needs to read an environmental variable, ORAHOME, in order to run. Her problem is that the variable needs to persist after the program is finished running. Her current solution is to run it from ksh, ". oraenv -d <orahome>".

Her question to me was, "Can I do this in Perl?" She wants to write a program in Perl, which will run "oraenv" and also interact with her database. For this, and several other reasons, she needs a solution using Perl.

This entry in perlfaq8 describes how this *could* be done, but I am not really experienced enough to fully understand what is described there. It has been my understanding that no child process could modify its parent's environment. Period.

But, this seems to intimate that I am wrong.

Any ideas how to accomplish this using Perl? I understand why system, backticks and exec aren't the answer(s).
  • Comment on Modifying ENV variable of parent process

Replies are listed 'Best First'.
Re: Modifying ENV variable of parent process
by bikeNomad (Priest) on Jun 03, 2001 at 02:03 UTC
    You can print expressions from your Perl script that, when eval'd in a shell script, change that shell's environment.

    So if you're using ksh, you could make an alias or function in the .profile that looks something like this:

    alias oraenv="eval \$(perl -e 'print \"export ORAstuff=\$\$\"')"
    Of course, the quoting can get annoying.
Re: Modifying ENV variable of parent process
by wog (Curate) on Jun 03, 2001 at 02:11 UTC
    If I understand you question correctly, you could attempt a solution where you run ksh from backticks, and have ksh tell you what it's environment is. For example:

    { my @lines = `/bin/ksh -c ". oraenv -d; set"`; for (@lines) { next unless /=/; my($k,$v) = split /=/, $_, 2; $ENV{$k} = $v; } }

    That should call /bin/ksh so it runs the script and then it has /bin/ksh run set, which dumps all the variables of the shell, and the results of the set should then be parsed and inserted into %ENV

    I forewarn that that code is untested.

Re: Modifying ENV variable of parent process
by virtualsue (Vicar) on Jun 03, 2001 at 02:17 UTC
    You can only fake such a change, which is one of the reasons why people seldom ever bother to do that. The usual (and IMO sensible) way to handle this type of situation is to write a simple wrapper in your favorite language around the applications which correctly specifies the environment that they need in order to run properly.
Re: Modifying ENV variable of parent process
by Vynce (Friar) on Jun 03, 2001 at 01:56 UTC

    this is not the solution you were looking for, but why not just export the correct value for ORAHOME in her login script? (the exact methodology will depend on her shell of choice, of course, but it's always possible).

    .
      I should have been clearer about that. AHOME may or may not be defined prior to running "oraenv". Also, it's contents will vary, depending upon which database she is interacting with.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-05-23 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found