in reply to Re: Windows environment variable not set if calling from a perl program (goto :end)
in thread Windows environment variable not set if calling from a perl program

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.
  • Comment on Re^2: Windows environment variable not set if calling from a perl program (goto :end)

Replies are listed 'Best First'.
Re^3: Windows environment variable not set if calling from a perl program (call)
by tye (Sage) on Dec 08, 2015 at 16:11 UTC

    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?
        How can I include "call setEnv.bat" inside a subroutine of perl

        Read more carefully? 'add "call setEnv.bat" to the preamble'

        If having the environment variables always set is a problem, then "you can't get there from here".

        - tye        

Re^3: Windows environment variable not set if calling from a perl program (goto :end)
by BrowserUk (Patriarch) on Dec 08, 2015 at 16:31 UTC
    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"
        If I invoke the batch file from inside another batch file - I can do this.

        Yes. Because running a batch file from another batch file does not start a new process. cmd.exe is the batch processor, so the changes are made to the current process' environment; and when you exit the called batch file, you are still running under the auspices of that same process. Hence, you can see the changes.

        But, when you call a batch file from perl, perl has to start a copy of cmd.exe to execute it; so the changes are made to cmd.exe's copy of the environment, but as soon you return to perl, that process and its environment is thrown away and you return to the same environment you had before you started that child process.

        What you are trying to do is impossible.


        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.