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

Hi All, I am having one bat file which will set up some environment variables. I want to use those variables with in the perl file. I tried with system call in perl. It is running the bat file, once it returned back to perl it is not remembering the environment variables. Any help to solve this issue? Thanks in adavance, Sowndar

Replies are listed 'Best First'.
Re: Getting the ENV from the .bat file
by Corion (Patriarch) on Feb 18, 2009 at 14:05 UTC

    You will need to adapt Get default login environment to your situation. Likely this involves using cmd /c your-batch-file.bat && set to output the environment.

      Sorry, I am not understanding. If I am having the bat file my_env.bat which have the value "set MY_HOME=C:\sowndar set VAR1=12". Does the perl will call some this like this, cmd /c "my_env.bat"; print $ENV{MY_HOME}."\n". $ENV{VAR1}; # Will this give proper value

        What you wrote has no semblance to the code in the nodes I linked. Please read Get default login environment.

        As to your code, did you try it? If so, what were the results?

Re: Getting the ENV from the .bat file
by ikegami (Patriarch) on Feb 18, 2009 at 14:10 UTC

    Each process has its own environment it inherits from its parent when its created. You can't change another process's environment.

    The usual trick is to have the batch file display its environment and have the caller parse that.

      Agreed. You're doing things back to front. The batch file sets up the environment then runs the Perl script, not the other way round.

      If you want to do it the other way round the batch file is actually a configuration file, so just read in values e.g.
      host:myhost.somewhere.com
      port:389
      basedn:dc=myldap

Re: Getting the ENV from the .bat file
by ELISHEVA (Prior) on Feb 18, 2009 at 16:07 UTC

    Forgive me if I am confusing you with someone else, but didn't we discuss this a few days ago in the chatter box? If so, I know it is sometimes hard to follow what is happening in the chatterbox, so I'll repeat here what we suggested. If I recall, the batch file is something you inherited and can't change. So we suggested that you wrap the batch file in a second batch file that looks something like this:

    REM run the script that sets environment variable FOO call foo.bat REM now %FOO% is set in the environment REM Corion;s suggestion: if you need the entire environment REM set called by itself dumps one line each VAR=VALUE set

    If dumping all environment variables is too much, you could just dump selected environment variables like this:

    REM run the script that sets environment variable FOO call foo.bat REM now %FOO% is set in the environment REM my suggestion: send just a few to stdout echo FOO=%FOO% echo BAR=%BAR%

    Then in your Perl script, you call your second batch file like this:

    use strict; use warnings; #backticks convert anything sent to stdout into a string #so this should fill $sEnvironment with assignments my $sEnvironment=`wrapper.bat` #now parse $sOutput and store the various VAR=VAL pairs my @aAssignment=split(/[\r\n]+/,$sEnvironment); foreach my $sAssignment (@aAssignment) { $sAssignment =~ /^(\w+)=(.*)$/; $ENV{$1}=$2; }

    Did you try that? What problems did you find?

    Best, beth

    Update: fixed error in call to foo.bat, identified by Corion via private message.