svasa has asked for the wisdom of the Perl Monks concerning the following question:

I have a batch script setEnv.bat that sets environemtn variable say TEST_ENV to true. Then I call this batch script from inside a perl script like my $batch_cmd = "setEnv.bat"; system( $batch_cmd ); Then I run the perl script from the command line, the setEnv.bat sets the environment variable. After the perl script completes, if I do "set TEST" it says: Environment variable TEST not defined. This means that the environment variable is lost when the batch script returned in the perl execution. How to solve this?
  • Comment on Windows environment variable not set if calling from a perl program

Replies are listed 'Best First'.
Re: Windows environment variable not set if calling from a perl program (goto :end)
by tye (Sage) on Dec 08, 2015 at 01:52 UTC
      This will work if an existing variable needs to be modified. My problem is that I want to set a new environment variable by invoking a batch file from a perl script. I don't know what's inside batch file before invoking the batch file. So I cannot use work around of $ENV{'envvariable'} = 'soandso'; in perl. I also tried pl2bat function converting my whole perl script to a batch file, it ran successfully but the behavior of environment variables is the same as before. The enviroment variables are not set.

        I did not say it solved your problem; I said that it shows how you can accomplish this.

        It sounds like the easiest solution (given that you are so reluctant to admit that you can read the contents of your setEnv.bat) would be to add "call setEnv.bat" to the preamble that pl2bat would add to your Perl script.

        - tye        

        I want to set a new environment variable by invoking a batch file from a perl script.

        A child process cannot change the environment of its parent! It simply cannot be done.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Windows environment variable not set if calling from a perl program
by NetWallah (Canon) on Dec 08, 2015 at 06:18 UTC
    Instead of "set TEST", use "setx", as explained in this stackoverflow article.

            Our business is run on trust. We trust you will pay in advance.

      That is not doing what the OP is expecting; and could cause mysterious problems.

      Rather than simply modifying the current session's environment; it makes persistent changes to the system registry. Which is fine if that is the intent; but dangerous if not.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Sorry, that won't work in my use case. I dont' want to set system-wide envs.
Re: Windows environment variable not set if calling from a perl program
by BrowserUk (Patriarch) on Dec 09, 2015 at 05:49 UTC

    There are two possibilities to do what you are trying to do:

    1. Have whomever writes the batch files that change the environment variables switch to writing short perl scripts that do it;

      and then invoke those batch-file-substitute scripts from perl using do setEnv.pl

    2. write a batch file parser in Perl, and interprete the batch scripts yourself.

      Be aware that despite its apparent simplicity, batch script is surprisingly complex.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks. Unfortunately not everybody who writes those batch scripts can write perl scripts. What I am doing for now is I have a windows batch script which calls perl program periodically ( basically perl became an utility now rather than the driving program ) the batch script is the driving program now. In this case I can set env variables. But yes writing such a complex program in batch is excruciatingly painful. I find it very easy in Perl.