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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling batch file global variables in Perl script

Status
Not open for further replies.

sappleg

Programmer
Apr 15, 2008
31
0
0
CA
Hi Guys,
I have a batch file containing the global variables that I am calling within the second batch file. Now, I want to call these global variables in Perl script instead of the second batch file. How to do that?

Here's what I am doing (two steps):

1. I have a batch file called: set_global_variables_odb.bat that contains:

SET gOracle_SID=ABCD
SET gOPWD_AGE_RATE=EFGHI
SET gOPWD_admin=JKLMNO

2. In the calling batch file I am doing:

call Set_Global_Variables_ODB.bat

echo %gOPWD_admin%
echo %gOPWD_AGE_RATE%
echo %gOracle_SID%
pause
----------------------------------------------
Now, I want to use perl instead of (2) calling batch file to call these variables. Does someone know how to do that in Perl? Sorry, I am new to Perl.

Thanks a bunch in advance.
G
 
If you "call" the batch file from perl, it will run a cmd.exe to process the batch file, which will set the variables, then exit and forget them again.

One way is that you could read the batch file into perl using the normal file I/O, and assign the variable manually to the %ENV hash.

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] VARS,[red]"[/red][purple]Set_Global_Variables_ODB.bat[/purple][red]"[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<VARS>[red])[/red] [red]{[/red]
        [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
        [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]set ([purple][b]\S[/b][/purple]+)=(.*)[/purple][red]/[/red][red])[/red] [red]{[/red]
                [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]setting [purple][b]\$[/b][/purple]ENV{[blue]$1[/blue]}=[blue]$2[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
                [blue]$ENV[/blue][red]{[/red][blue]$1[/blue][red]}[/red]=[blue]$2[/blue][red];[/red]
        [red]}[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] VARS[red];[/red]

[gray][i]# check that it's set in %ENV[/i][/gray]
[black][b]print[/b][/black] [red]"[/red][purple][blue]$ENV[/blue]{gOracle_SID}[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[gray][i]# check that it's inherited by child processes[/i][/gray]
[url=http://perldoc.perl.org/functions/system.html][black][b]system[/b][/black][/url] [red]'[/red][purple]cmd /c "echo %gOracle_SID%"[/purple][red]'[/red][red];[/red]

Annihilannic.
 
Thanks. It worked :). Appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top