Jack Faley ( The Tao of Jack ) wrote:
>
> On 9/9/06, john wright <[EMAIL PROTECTED] wrote:
>>
>> *"Jack Faley ( The Tao of Jack )" < [EMAIL PROTECTED] wrote:
>>>
>>> On 9/8/06, john wright wrote:
>>>>
>>>> I want to call vcvars32.bat from perl script and effectively borrow the
>>>> variables from that batch file. If I use system call, the variables set in
>>>> the batch file are lost when the execution come out of the system  call .
>>>
>>> Do these variables change while perl is running? Otherwise you could set the
>>> variables in the batch file and have it call perl.
>>
>> I am calling vcvars32.bat using system call in perl program,but the variables
>> set in the batch file are lost when the execution come out of the system
>> call.i want  preserve all variables values define in the vcvars32.bat in my
>> perl program after executing the vcvars32.bat.
>
> Perl should inherit whatever the environment shell it is called in. Id be
> interested to know if you listed you environment variables before calling perl
> if they are there. I just had to write code for perl last week ( on UNIX)
> concerning environment variables. It was being called from a program where the
> needed shell environments which weren't weren't set but I needed to have them
> to access DB2 and only had one line so I had to first source the profile to
> get trhe environments in the current shell then use a semicolon to be able to
> run two commands on the same line ( a limitation of the app) i.e.
>
> . ./home/db2/.profile;/usr/local/bin/script.pl
>
> Am I correct to understand that on the dos prompt if you tpe set it shows the
> correct environment variable. You then call a batch file that does a few
> commands then calls perl and perl doesn't have the environment variables? It
> should unless the batch file commands prior to it span a new unitialized shell
> for some reason. I guess you could either tell us what the batch is doing
> before calling perl OR pass the environment variables into perl as arguments
> when you call perl or by redirecting set to a file then reading in the file
> from perl i.e. set  set.out then in perl OPEN set.out with a filehandle and
> read the value you need.
>
> Is the real problem that when a new dos command shell is created by perl,
> THAT NEW DOS shell which is created by perl somehow doesn't have the same
> environment variables?

Hello

(Jack, John, please bottom-post your answers. This thread was getting
incomprehensible even with just two posters!)

You're right Jack, system() call (and backticks) generates a separate temporary
sub-process. Any environment variables set in that process are independent of
the Perl program, and will be discarded when that process exits.

The easiest way I can see to make the stuff set up by vcvars32 visible to Perl
is to put both in a single batch file, but remember that the changes made by
vcvars32 will remain for the period of the login. (If you're happy with that,
why not just put vcvars32.bat in the Startup folder, so that the values are
present at every login. Heck, put it in autoexec.bat like I have - I can't see
any damage being done!)

If you really want to set up the variables only at runtime, then how about
something like the program below, which parses the output of the 'set' command
to produce a new version of the %ENV hash. This hash alters the live
environment, so that anything currently run using system() will see these new
values, but the changes are invisible outside the Perl process and its children.

The program also lists all changes to the environment variables (except for any
deletions, which vcvars32 doesn't do) for interest's sake, but this part isn't
in any way necessary and can be removed.

HTH,

Rob



use strict;
use warnings;

my @set = [EMAIL PROTECTED] & set`;
chomp @set;

my %set = map /(\w+)=(.+)/, @set;

foreach my $k (keys %set) {
  if (not exists $ENV{$k}) {
    print "New variable $k = $set{$k}\n";
  }
  elsif ($set{$k} ne $ENV{$k}) {
    print "Old variable $k altered from $ENV{$k} to $set{$k}\n";
  }
}

%ENV = %set;





































--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to