0

Let's consider the following perl script:

#!/usr/bin/perl
system("C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/Common7/Tools/VsDevCmd.bat");
system("msbuild");

The batch file invoked with the first system call is supposed to set up some environment variables so that the msbuild executable in the second system call can be found.

When I run this perl script I get the following error:

'msbuild' is not recognized as an internal or external command, operable program or batch file.

So it looks like the environment variables set in the batch file are not made available to the context of the perl script. What can I do to make this work?

Note 1

Running first the batch file from a console window and then running msbuild works fine. So the batch file works as expected and msbuild is actually available.

Note 2

My real-world perl script is much longer. This example here is a massive simplification which allows to reproduce the problem. So I cannot easily replace the perl script with a batch file, for example.

Note 3

The funny thing is: I've been using this perl script for one or two years without any problems. Then suddenly it stopped working.

4
  • Is the first command (with spaces in the path) actually running correctly? See my answer to Execute Windows command with spaces in Perl for instance. Commented Sep 6, 2018 at 9:20
  • Yes the first command seems to run fine. I can see that batch file printing some banner to the console. Commented Sep 6, 2018 at 9:23
  • 1
    You create a shell, execute a batch file which does stuff like change that process's environment, then exit the process you changed. You need to execute both commands in the same shell somehow. Commented Sep 6, 2018 at 9:36
  • And how would I do that in a perl script? Commented Sep 6, 2018 at 9:47

1 Answer 1

4
  • Your process has an associated environment which contains things like the search path.
  • When a sub-process starts, the new process has a new, separate, environment which starts as a copy of the parent process's environment.
  • Any process (including sub-processes) can change their own environment. They cannot, however, change their parent's process's environment.
  • Running system() creates a new environment.

So when you call system() to set up your environment, it starts a new sub-process with a new environment. Your batch program then changes this new environment. But then the sub-process exits and its environment ceases to exist - taking all of the changes with it.

You need to run the batch file in a parent process, before running your Perl program.

Sign up to request clarification or add additional context in comments.

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.