[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

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...
  • 4
  • 2
  • 2
  • +1
3 Solutions
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>;;
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 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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now