http://www.perlmonks.org?node_id=931286


in reply to Issue with env variables set through dos batch

I'm not really familiar with DOS batch files, but on linux a shell script (the equivalent to a batch file) is run in a new process. And when the process exits, its environment variables vanish with it.

There you need to launch the perl script that is supposed to see the environment variables from within the shell script, so that it is a subprocess of the shell which holds the interesting environment variables.

To check if that's the case in DOS/Windows too, simply inspect the environment variables on the dos prompt after you ran the batch file.

Replies are listed 'Best First'.
Re^2: Issue with env variables set through dos batch
by akrrs7 (Acolyte) on Oct 13, 2011 at 16:10 UTC
    Very true...that is exactly the behavior I'm observing in the windows cmd window. How do I get the env. variables to stay put ? Running the perl script from a batch script is not right now an option for me.
      In general if you set ENV variables in a shell and then launch a program from that shell's environment, the program will inherit the environment variables.

      I you want some ENV variables to apply to any command line window, Go to Control Panel|System|Environment Variables, change them and then set them "to apply to all future command line windows". This is for example, how you would set "PATH" to be persistent.

      If this is a "one off" thing, then I would pass special settings as part of @ARG.

Re^2: Issue with env variables set through dos batch
by ikegami (Patriarch) on Oct 13, 2011 at 16:45 UTC
    While environment variables are inherited from parent to child like in Linux, a batch files are run more like source foo.bat than cmd /c foo.bat when possible (when the parent is cmd).

        The parent is perl, which can't execute batch files, so a cmd is indeed launched here.

        I wasn't so much correcting you as explaining that the behaviour is different than explained when the batch file is run from the prompt.

Re^2: Issue with env variables set through dos batch
by Marshall (Canon) on Oct 13, 2011 at 15:51 UTC
    There is no DOS anymore.

    The Windows command prompt is like one of the ~unix shells. for example:

    #------------------------------------------ my $operatingSystem = $^O; my $windows = "MSWin32"; my $unix = "hpux"; print "Running under ${operatingSystem} environment.\n\n"; my $pathSeparator = ""; if ( $operatingSystem eq $windows ) { $pathSeparator = "\\"; } else { $pathSeparator = "/"; } #------------------------------------------
    is not needed!

    pathSeparator of "/" works on Windows and ~Unix (LINUX, etc..)

    Where it really matters, the logic above to determine the OS is woefully inadequate.

      I really appreciate your input..however it does not answer my question on the environment variables. I'm not really concerned about the part of the perl script with the check for the os...I'm not using that $pathSeparator variable anywhere.