Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

How to set environment variables using C/C++ through PERL script

Highlighted
Frequent Advisor

How to set environment variables using C/C++ through PERL script

Hi All,

I want to run a PERL script through my C/C++ code. In the PERL script i am generating a SHELL script that sets some environment variables. As discussed previously in the thread,


But, now my question is can we run this PERL script through the C/C++ code. Because i think it is not possible to run an executable as Source (Please correct me if i am wrong).

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1164715

Please let me know the solution for this.


thanks in advance
Vikram
5 REPLIES
Honored Contributor [Founder]

Re: How to set environment variables using C/C++ through PERL script

Hi,

you're right - this is not possible.
But since you already create a file for the environment variables and theier values:
Just create them in the form
var1=value for it
var2=other value for that

Then in your C-code read this file line-by-line and feed the string into putenv().
Codefragment (no error checking):

FILE *ef = fopen("envfile","r");
char str[128];
while (fgets(str,127,ef) putenv(str);
fclose(ef);

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Honored Contributor [Founder]

Re: How to set environment variables using C/C++ through PERL script

If you call perl (system? fork?) then you are in a different prcoess once in perl.
You can set all the environment variables you like for that process, but they will not be visible to the current process running the C code.

WHY do you think you need to go trhough perl to sh to do this?

WHAT is the value the perl step adds?

WHY not do it all directly in the C program, callign putenv?

hth,
Hein.

Frequent Advisor

Re: How to set environment variables using C/C++ through PERL script

Hi All,


But, putenv amd getenv will work inside the program. Because i think when we execute a C/C++ program separate process is started then whatever ssettings we do will be there in that Process. So after completion of execution if i do

echo $HELLO

where "Hello=Hai" string is present ia file and this string i am reading from that file.

Is there any way i can achieve this.

This i want because we already have a PERL script that is generating the file containing the environment variable list.
Can any body please help me out of this Problem.

thanks and regards
Vikram
Acclaimed Contributor [Founder]

Re: How to set environment variables using C/C++ through PERL script

Hi Vikram:

This thread (as you noted) is a continued discussion of your previous thead.

> ...we already have a PERL script that is generating the file containing the environment variable list.

Then, in your C/C++ program read the generated file of environmental variables; parse the variable and associated value and issue 'putenv' calls.

Peter, in fact, has already shown a code snippet to do this. His C/C++ suggestion is the same as my shell suggestion in you earlier thread. There isn't any more magic here. Let your Perl script generate a common environmental configuration file for you shell process (in shell syntax) and let your C/C++ program conform to parsing and using that format to apply 'putenv' calls.

Regards!

...JRF...
Acclaimed Contributor [Founder]

Re: How to set environment variables using C/C++ through PERL script

If I under stand your question then the answer is no and thankfully no one can help you out of this --- and it is a very good thing that they can't. You want the enviroment of a child process to be inherited by a parent process. The child process inherits the parent's environment not the other way round.

Your Perl script could do something like write to stdout a series of strings "AAA=aaa\n","BBB=bbb\n", .... and if your Perl script were called by popen() for example, it would be able to then call putenv() using each of these strings. That (or a similar scheme using a temporary file) is the only way a child can alter a parent's environment because it is doing it indirectly.


If it ain't broke, I can fix that.