Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Avoiding shell escapes

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
I'm using Perl v5.6.1 under WinNt 4.0 in a Dos box.
Inside the Perl script, I need to call a batch file (<name>.bat), which sets all kinds of things - what it actually does is beyond my scope. I need those settings to continue with my own Perl script.

My problem: this batch file is invoked in a different shell, i.e. all the environment settings are copied, and using this copy, the batch file is executed. It is also on that copy that changes are made. When the program ends, the copy is destroyed, leaving Perl with an unmodified environment, which is, in this case, highly indesirable.

Is there any way to circumvent this, I mean, to let the batch file make the changes on the same environment as my Perl script uses?
 
Hi,

If all your .bat file does it set environment variables you could open() the .bat file, read it line by line and set the variable in the %ENV hash based on what is in the .bat file.

Variables are set in DOS like this

set varname=value

So, your perl script would look something like this:
[tt]
open(B, 'setvars.bat) || die $!;
while(<B>){
chomp; # we don't need the newline character
s/set //; # because we don't need the 'set' command
($var, $value) = split(/=/);
$var=uc($var); # don't know if this is needed,
# but will not hurt
$ENV{$var} = $value;
}
# the rest of your code here
[/tt]
Once you've run this code, you can run other system() commands, backtick commands and open() commands with the correct environment. Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Hmmm.

If your .bat file does other stuff as well, you can get at just the 'set' lines by inserting this line after the while() line

next unless /^\s*set /; Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top