0

I am writing a cross-platform build script in Perl for code compilation.

In Windows, I need to execute vsvars32.bat to set the environment variables:

sub set_win_env {
      #MSVC version 9.0 is installed
      $VS90COMNTOOLS = $ENV{'VS90COMNTOOLS'};
      $VS90COMNTOOLS .= "vsvars32.bat"
      if($VS90COMNTOOLS ne "") {
          system("$VS90COMNTOOLS");
      }
 }

The environment variables set by executing the batch files gets lost as interpreter spawns another shell to execute the batch file.

How can I import those variables in the parent Perl script?

2
  • BTW, how a variable can be an empty string after being concatenated with a constant string? The condition is always true. Commented Nov 5, 2012 at 12:44
  • yeah useless check... initially i was appending the variable after the check... :) Commented Nov 5, 2012 at 12:49

2 Answers 2

1

If your BAT file is simple (i.e. it only contains lines of the form VAR=VALUE), you can parse it by Perl. If there is something more going on (some values are calculated from others etc.), just output all the variable names and their values at the end of the BAT script in the form VAR=VALUE.

The parsing of the simple format is easy:

while (<$BAT>) {
    chomp;
    my ($var, $val) = split /=/, $_, 2;
    $ENV{$var} = $val;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried reading the batch file in form of key=value. However, it seems that the batch file(its a msvc file) has values of the form %VAL%=<path>:%VAL%. In this case i am not bale to read the correct and complete values. And i cannot modify the batch file. only can read that,
1

As you say, using system will set the environment variables in the child process, not in the current process. The problem is that you have a file in '.bat' format, which is a different language to Perl (OK, maybe calling it a language is a bit strong).

You need to either parse the file yourself and translate it into Perl, or, run your perl from a .bat file which has (for example):

CALL "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" amd64
perl perlscript.pl

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.