Link to home
Start Free TrialLog in
Avatar of NewtonianB
NewtonianB

asked on

perl calling batch that sets environment variables

myperl.pl
system("env.bat");
system("myApp");

I have a perl script which calles a bat file. This at fille is supposed to set some environment variables the following way.
env.bat
set BLA="blabla"
set BLA2="bla2bla2"

however it does not seem to be working, after runing the batch file it makes no difference...
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NewtonianB
NewtonianB

ASKER

It seems to be doing something but for some reason its not quite working the same way as when I run this through the command line.
Is there anything else I should take into consideration to make this working the same way as command line?
given that some of my paths are like this
set BLA=\bladir
set BLA2=\bla\bla\dir

They are relative to the drive example (X:) which I've substituted.

So in my script i do a change working directory to X: then run the above script to execute the environment variable, is there anything else i need to do to have the same setup given the relative and substituted drive?

The proces to get the application im trying to run working is to
subst X: c:\bla\bla
cd X:\
X:\env.bat
X:\bla\bla\bla\script.pl

right now all i do is
myscript.pl
- changes directory to X:
- read env.bat and execute
- try to run the program script.pl runs

but something doesnt seem to be working quite right in the program its trying to run, sorry for not being able to give concrete detail but the app is not public
Ok I found the problem
one of my entries in env.bat is
set PATH=\blabla;%PATH%

well the original %PATH% does not seem to get reassigned so right now in perl im hacking it to say if its PATH save it and reappend it a tthe end. Whats the right way of doing this?
the right way is probably to run the ap in the same process as the bat or a child process of the bat.
letting your shelll process the bat with whatever shell features the bat wants to invoke, without perl having to emulate it,
depending on how your shell works, it might be done like

system("env.bat && myApp");

If you want perl to process %PATH%, one way to do it might be
 while( <$bat> ){
  s/%(\w+)%/$ENV{$1}/g;
  /\bset\s+(\w+)=(.*)/ and ENV{$1}=$2;
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
t0t0 if I put everything on line line like you suggested with exit /b don't I need to change the parsing method ozo suggested as I beleive he is going line by line
the proper way to set the system PATH is to modify it's entry in the registry file - if that's what you're trying to do.

if you want to set some variables in a batch file and make them available to other commands after the batch file ends then you need to set them as I described above.

Pleae explain what it is you are wanting to parse...

Overall, please explain what it is you are trying to achieve.... and what role your perl program plays. Is your perl program necessary? Do you need to use perl or can you make do with DOS/Windows batch file programs?
 

thank you so much it works, i am just simulating the environment in my perl process