[Webinar] Streamline your web hosting managementRegister Today

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

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...
0
NewtonianB
Asked:
NewtonianB
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
ozoCommented:
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>;;
0
 
Adam314Commented:
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+)=(.*)$/;
	$ENV{$1}=$2;
}

Open in new window

0
 
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:\
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
0
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?
0
 
ozoCommented:
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;
}
0
 
t0t0Commented:
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.





0
 
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
0
 
t0t0Commented:
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?
 

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

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