perl calling batch that sets environment variables

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

however it does not seem to be working, after runing the batch file it makes no difference...
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

the batch file is run in a child process, and the batch file changes the environment of that child, not its parent.
when the child process exits, its environment is lost.
If you can't run the bat and the app in the same child, or the app as a child of the bat,
then if the format of the bat file is known to exactly as you presented, you might parse it with perl to changes perl's environment:
open my $bat,"<","env.bat" or die $!;
/\bset\s+(\w+)="(.*)"/ and ENV{$1}=$2 while <$bat>;;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If the batch file is not so easily parsed, you could run the set command in the same process as the batch file, and process the output from that.

my @lines=`env.bat && set`;

foreach (@lines) {
	next unless /^(\w+)=(.*)$/;

Open in new window

NewtonianBAuthor Commented:
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:\

right now all i do is
- changes directory to X:
- read env.bat and execute
- try to run the program 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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

NewtonianBAuthor Commented:
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> ){
  /\bset\s+(\w+)=(.*)/ and ENV{$1}=$2;
To answer the question given the code above, you need to do this:

   @echo off
   set BLA="blabla" & set BLA2="bla2bla2" & exit /b

That will set your environment variable BLA and BLA2 upon exiting the batch file. Therefore,

   @echo off
   set BLA=\bladir & set BLA2=\bla\bla\dir & exit /b

Should work as well.

NewtonianBAuthor Commented:
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?

NewtonianBAuthor Commented:
thank you so much it works, i am just simulating the environment in my perl process
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.