title: Why aren't a batch file's environment variables set when run from Perl?
body: <p>I am trying to invoke a bat file from a perl script as follows:</p>
  
  <pre><code> system("call D:/SIP/run_$file_idx.bat");
  </code></pre>
  
  <p>However I observe that the environment variables in the bat fail to get resolved. 
  If I run the bat file separately from the command prompt it works.</p>
  
  <p>Does system() create a new environment and execute bat file in that?
  What is that I am missing?</p>
  
  <pre><code>if (($ENV{'IPSL_RUN_FLAG'}) eq "TRUE") {
  
  my $Book = $Excel-&gt;Workbooks-&gt;Open(
      "$ENV{'IPSL_TESTCASES_PATH'}IPSLFeatureLoadRunResults.xls"); 
  
  # Make transparent where the IPSL tarball is installed. 
  # Have ControlPanel save results here.
  
  # You can dynamically obtain the number of worksheets, rows, and columns
  # through the Excel OLE interface.  Excel's Visual Basic Editor has more
  # information on the Excel OLE interface.  Here we just use the first
  # worksheet, rows 1 through 4 and columns 1 through 3.
  
  # select worksheet number 1 (you can also select a worksheet by name)
  
  my $count=0;
  
  my $Sheet = $Book-&gt;Worksheets("LOADDATA");
  my $tmp=0;
  
  
  foreach my $row (13..776) {
      foreach my $col (17..17) {
          if(($Sheet-&gt;Cells($row,$col)-&gt;{'Value'} eq "Failed") || 
              ($Sheet-&gt;Cells($row,$col)-&gt;{'Value'} eq "No Run") ) {
              $cnt_of_current_rerun_testcases++;
              foreach my $col (18..18) {
                  # skip empty cells
                  next unless defined $Sheet-&gt;Cells($row,$col)-&gt;{'Value'};
                  my $a = $Sheet-&gt;Cells($row,$col)-&gt;{'Value'};
  
                  my $i = index($a, 'run');  
                  $a = substr($a, 0, $i); #remove runTest*  
  
                  print OUT "\n";
                  if($count == 0) {
                      print OUT "\nREM "; 
                      print OUT  "*" x 100; 
                      print OUT "\n";
                      print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ips32.exe";
                      print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ipsldb.exe";
                      print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ipsltiu.ex\n";    
                  }
  
                  print OUT "c:\n";            
                  print OUT "\ncd ";
  
                  $a =~ s/%I/\"%I/g;
                  $a=~s/H%/H%\"/g;
  
                  print OUT " $a\n";
  
                  print OUT "\n";
                  print OUT "CALL run_SubscribeFeatureOnHIQ.bat";
                  print OUT "\n";
  
                  print OUT "sleep 10\n";
  
                  print OUT "\ncd ";
                  print OUT " $a\n";
                  print OUT "\n";
  
                  print OUT "CALL ";
                  $i=$Sheet-&gt;Cells($row,$col)-&gt;{'Value'};
                  $i=~ s/%I/\"%I/g;
                  $i=~s/H%/H%\"/g;
                  print OUT $i;
                  #print OUT  $Sheet-&gt;Cells($row,$col)-&gt;{'Value'};
                  print OUT "\n";
  
                  $count++;
                  if($count == $no_simul_tcases) {
                      $sleep_cnt++;
                      print OUT "echo Going for sleep $sleep_cnt\n";
                      print OUT "SLEEP 300";
                      print OUT "\n";
                      $count=0;
                  }
              }
          }
      }
  }
  
  print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ips32.exe";
  print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ipsldb.exe";
  print OUT "\ntaskkill /F /FI \"USERNAME eq %USERNAME%\" /IM ipsltiu.ex\n";         
  print OUT "\nset IPSL_RUN_FLAG=TRUE";
  close OUT ;
  system(\"start $ENV{'IPSL_TESTCASES_PATH'}SIP\\run_$file_idx.bat\");
  </code></pre>
  
  <p>And the batch file is:</p>
  
  <pre><code>taskkill /F /FI "USERNAME eq %USERNAME%" /IM ips32.exe
  taskkill /F /FI "USERNAME eq %USERNAME%" /IM ipsldb.exe
  taskkill /F /FI "USERNAME eq %USERNAME%" /IM ipsltiu.ex
  c:
  
  cd  "%IPSL_TESTCASES_PATH%"SIP\TestCASE\FEATURESINT\INT_cfSRS\INT_cfSRS_cfSERRNG\
  
  CALL run_SubscribeFeatureOnHIQ.bat
  sleep 10
  
  
  cd  "%IPSL_TESTCASES_PATH%"SIP\TestCASE\FEATURESINT\INT_cfSRS\INT_cfSRS_cfSERRNG\
  
  CALL "%IPSL_TESTCASES_PATH%"SIP\TestCASE\FEATURESINT\INT_cfSRS\INT_cfSRS_cfSERRNG\runTest_SRS_INT_SERRNG.bat
  </code></pre>
  
  <p>Possible workaround:</p>
  
  <p>By using exec and specifically writing the ENV into bat file gave me a workaround:</p>
  
  <pre><code>print OUT map { "set $_=$ENV{$_}\n" } 
            qw( path USERPROFILE USERNAME ProgramFiles ComSpec APPDATA );
  </code></pre>
  
  <p>The issue is still seen with system() though. I tried Sinan's suggestion in the answerers..</p>