in reply to Windows environment variable not set if calling from a perl program

Re: vienv - Edit local environment variables on Win32 (fixed) shows how to accomplish this.

- tye        

  • Comment on Re: Windows environment variable not set if calling from a perl program (goto :end)

Replies are listed 'Best First'.
Re^2: Windows environment variable not set if calling from a perl program (goto :end)
by svasa (Initiate) on Dec 08, 2015 at 16:01 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        

        Thanks for the reply. How can I include "call setEnv.bat" inside a subroutine of perl - the pl2bat sure generated a .bat file, but the subroutines are still subroutines right? Can I just stick in a statement "call setEnv.bat" inside a subroutine in the generated bat file?
      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.
        If I invoke the batch file from inside another batch file - I can do this. Lets say I have batch2.bat the inside of which looks like: @echo off set TEST_ENV=true I have another batch file setEnv.bat the inside of which looks like: @echo off call batch2.bat after executing setEnv.bat on command line if I do "set TEST" it will print: TEST_ENV=true The same thing if I do from perl like : my $cmd = "call setEnv.bat"; system( $cmd ); and call the perl script from command line. then do "set TEST" It will print : "Environment variable TEST not defined"