Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Issue with env variables set through dos batch

by ikegami (Pope)
on Oct 13, 2011 at 16:55 UTC ( #931318=note: print w/ replies, xml ) Need Help??


in reply to Issue with env variables set through dos batch

Batch files need a shell to execute them, so Windows will launch a new cmd to execute the batch file when you do system("$scriptDir/dosTest.bat");. Similarly, executing the shell script in linux will launch the shell listed in the #! line. The batch file or shell script is then executed by this shell.

The batch file or shell script then proceeds to set the shells environment variables, which works.

Then the shell exits and Perl continues to execute. Nowhere did you change Perl's environment. Nor is it possible to do so.

At least not directly. The trick is to have the batch file or shell script tell your Perl script how to modify its environment, then have your Perl script obey the request.

:: test.bat @echo off echo FOO=foo echo BAR=bar
for (split /\n/, `test.bat`) { my ($k, $v) = split /=/, $_, 2; $ENV{$k} = $v; }

I'm guessing you can't run modify the batch file. If so, you can use the following strategy instead:

:: wrapper.bat @echo off call existing.bat set
:: existing.bat @echo off set FOO=foo set BAR=bar
for (split /\n/, `wrapper.bat`) { my ($k, $v) = split /=/, $_, 2; $ENV{$k} = $v; }


Comment on Re: Issue with env variables set through dos batch
Select or Download Code
Re^2: Issue with env variables set through dos batch
by akrrs7 (Acolyte) on Oct 13, 2011 at 17:25 UTC
    Hello Ikegami... Yes..I cannot modify the batch file. It is as it is and I was thinking of doing one of 2 things: 1. Small batch/shell script that first calls the env var batch/shell script and then my perl script - this would ensure that the env variables are set before my perl script is executed. 2. Use the File Open in Perl to read the batch/shell script and execute each line like a perl command....not sure if do-able ?

      You forgot 3. Use the solution I provided.

      (1) is the simplest.

        Hello Ikegami... Yes...thanks. That should be the easiest. I will try it out. Thanks...

      What you described in #1 should work. This is very similar to what happens with the portable version of Strawberry Perl.

      In the case of portable Strawberry Perl, there's batch script that you run first and it does two things: opens a command prompt window and sets the environment variables for just that command prompt window. From there, you can run your scripts and Windows will know where to find all of the Perl files (perl.exe, "installed" modules, etc.) for the portable Strawberry Perl.

      Also, I haven't tested it, but ikegami's suggestion looks like it should work too.

Re^2: Issue with env variables set through dos batch
by Anonymous Monk on Nov 06, 2012 at 06:25 UTC
    what will happen in case batch file have values like : %VAL%=<some path>:%VAL% i you read it as a key value pair, then the values that get appended are lost
      i[f] you read it as a key value pair, then the values that get appended are lost

      Not at all. The code line:

      my ($k, $v) = split /=/, $_, 2;

      splits each data line into two parts: the part to the left of the first = character — which is assigned to $k — and the entire part to the right of the first = character — which is assigned to $v. So nothing is lost. In your example, the batch file would contain:

      set VAL=<some path>:%VAL%

      and if VAL already contained, say, /bin, the result would be the assignment of VAL to $k and of <some path>:/bin to $v. See split:

      If LIMIT is specified and positive, it represents the maximum number of fields into which the EXPR may be split; in other words, LIMIT is one greater than the maximum number of times EXPR may be split.

      Hope that helps,

      Athanasius <°(((><contra mundum

      what will happen in case...

      It should take all of 10 seconds to find out :) Try It To See

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://931318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (18)
As of 2014-09-22 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls