10-11-2007 04:37 PM
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).
Please let me know the solution for this.
thanks in advance
10-11-2007 10:55 PM
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");
while (fgets(str,127,ef) putenv(str);
10-11-2007 11:43 PM
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?
10-12-2007 02:37 AM
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
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
10-12-2007 02:55 AM
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.
10-12-2007 03:02 AM
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.