CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Expanding environmental variables

 



Agis
New User

Jan 10, 2007, 3:55 AM

Post #1 of 6 (2146 views)
Expanding environmental variables Can't Post

Hi,

Yep, another newbie to the forum...

Suppose I have a file containing the environmental settings for a certain application:

C:\\perlsrc\\test\\env_test.dat

WL_HOME=C:\wlg\8.1\weblogic81
WLS_HOME=$WL_HOME\server

I read this file from within my perl script and set the environment accordingly:

#!/usr/local/bin/perl

$ENV_NAME=test

unless (open(ENVFILE,"C:\\perlsrc\\$ENV_NAME\\env_$ENV_NAME.dat")){
die ("cannot open input file $BUILD_HOME\\$ENV_NAME\\env_$ENV_NAME.dat\n");
}
$line = <ENVFILE>;
while ($line ne ""){
chop($line);
@tmp=split(/=/,$line);
$TEMPVAR=$tmp[1];
${eval($tmp[0])}=$TEMPVAR;
print ("$tmp[0] - $tmp[1] - ${eval('$tmp[0]')}\n");
$ENV{THISVAR}=$TEMPVAR;
$ENV{eval('$tmp[0]')}=$ENV{THISVAR};
$line = <ENVFILE>;
}
close(ENVFILE);
foreach $key (sort keys(%ENV)) {
print("$key = $ENV{$key}\n");
}


This does set the environment:

WLS_HOME = $WL_HOME\server
WL_HOME = c:\wlg\8.1\weblogic81

But the $WLS_HOME environment variable $WLS_HOME is not expanded.

How do I get this done?

Tia, Marco


KevinR
Veteran


Jan 10, 2007, 10:51 AM

Post #2 of 6 (2145 views)
Re: [Agis] Expanding environmental variables [In reply to] Can't Post

If you can rewrite the file, it might be easier to write it as a real perl data structure and use "do" to read it into your perl script.

http://perldoc.perl.org/functions/do.html
-------------------------------------------------


davorg
Thaumaturge / Moderator

Jan 11, 2007, 3:08 AM

Post #3 of 6 (2136 views)
Re: [Agis] Expanding environmental variables [In reply to] Can't Post

You seem to be making this far more complex than it needs to be. Why are you using all those calls to 'eval'?

I'd do this:


Code
#!/usr/local/bin/perl 

use strict;
use warnings;

my $ENV_NAME = 'test';

open(ENVFILE, "C:/perlsrc/$ENV_NAME/env_$ENV_NAME.dat")
or die "cannot open input file C:/perlsrc/$ENV_NAME/env_$ENV_NAME.dat - $!";

while (<ENVFILE>){
chomp;
@tmp=split(/=/, $_, 2);
$ENV{$tmp[0]} = $tmp[1];
}

close(ENVFILE);
foreach (sort keys(%ENV)) {
print("$_ = $ENV{$_}\n");
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Agis
New User

Jan 14, 2007, 11:56 PM

Post #4 of 6 (2126 views)
Re: [davorg] Expanding environmental variables [In reply to] Can't Post

Kevin, Dave,

Thanks for your replies. Been fiddling around a bit, but cant seem to get it right. Dave, tried your script, but at first I got the following error:
C:\PerlSRC>setenv.pl
Global symbol "@tmp" requires explicit package name at C:\PerlSRC\setenv.pl line 12.

I hacked the use strict; line, the script ran ok. Still it doesn't work like I intended. The environment variables are set, but they're unusable:
DB<1> x $ENV{WL_HOME}
0 'c:\\bea\\wlp8.1_sp5\\weblogic81'
DB<2> x $ENV{WLS_HOME}
0 '%WL_HOME%\\server'
DB<4> x system("dir %WL_HOME%")
Het volume in station C heeft geen naam.
Het volumenummer is D873-6F55

Map van c:\bea\wlp8.1_sp5\weblogic81

15-01-2007 08:48 <DIR> .
15-01-2007 08:48 <DIR> ..
15-01-2007 08:49 <DIR> server
15-01-2007 08:27 0 test.txt
1 bestand(en) 0 bytes
3 map(pen) 6.341.783.552 bytes beschikbaar
0 0
DB<5> x system("dir %WLS_HOME%")
Het systeem kan het opgegeven bestand niet vinden.
0 256

As you can see (never mind the dutch) the variable %WL_HOME% seems tio be set ok, I can do a dir of that directory. One other variable, %WLS_HOME% is build from the variable %WL_HOME%, extended with server. This directory also exists, but I can't list it ("Het systeem kan het opgegeven bestand niet vinden." means something like "the system can't find the path specified").

So, I'm missing something here (probably, being a perl rookie). Any more suggestions are greatly appreciated...

Marco


davorg
Thaumaturge / Moderator

Jan 15, 2007, 3:33 AM

Post #5 of 6 (2121 views)
Re: [Agis] Expanding environmental variables [In reply to] Can't Post


In Reply To
Dave, tried your script, but at first I got the following error:
C:\PerlSRC>setenv.pl
Global symbol "@tmp" requires explicit package name at C:\PerlSRC\setenv.pl line 12.

I hacked the use strict; line, the script ran ok.


A better approach would have been to fix the problem, rather than to stop Perl reporting the problem :)


Code
my @tmp=split(/=/, $_, 2);


"use strict" is there to protect you from making silly errors. You should get into the habit of _always_ using it in Perl programs.


In Reply To
Still it doesn't work like I intended. The environment variables are set, but they're unusable:
DB<1> x $ENV{WL_HOME}
0 'c:\\bea\\wlp8.1_sp5\\weblogic81'
DB<2> x $ENV{WLS_HOME}
0 '%WL_HOME%\\server'
DB<4> x system("dir %WL_HOME%")
Het volume in station C heeft geen naam.
Het volumenummer is D873-6F55

Map van c:\bea\wlp8.1_sp5\weblogic81

15-01-2007 08:48 <DIR> .
15-01-2007 08:48 <DIR> ..
15-01-2007 08:49 <DIR> server
15-01-2007 08:27 0 test.txt
1 bestand(en) 0 bytes
3 map(pen) 6.341.783.552 bytes beschikbaar
0 0
DB<5> x system("dir %WLS_HOME%")
Het systeem kan het opgegeven bestand niet vinden.
0 256

As you can see (never mind the dutch) the variable %WL_HOME% seems tio be set ok, I can do a dir of that directory. One other variable, %WLS_HOME% is build from the variable %WL_HOME%, extended with server. This directory also exists, but I can't list it ("Het systeem kan het opgegeven bestand niet vinden." means something like "the system can't find the path specified").

So, I'm missing something here (probably, being a perl rookie). Any more suggestions are greatly appreciated...


Looks like you need to do another level of substitution when evaluating the data in your file. I know nothing at all about how Windows environment variables work, but it looks like you're saying that any string of the form %xxx% should be replaced with the value of $ENV{xxx}.

Therefore I think that you need to change the main loop to be something like this (it's untested as I don't have access to Windows).


Code
while (<ENVFILE>){  
chomp;
my @tmp=split(/=/, $_, 2);
$tmp[1] =~ s/%(\w+)%/$ENV{$1}/eg;
$ENV{$tmp[0]} = $tmp[1];
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Agis
New User

Jan 15, 2007, 4:44 AM

Post #6 of 6 (2119 views)
Re: [davorg] Expanding environmental variables [In reply to] Can't Post

Dave,

That's it! About the strict, yes, offcourse you're right, I was unsure about how to get rid of the error message. Got that one right also...
And about windows, well, let's just say..., well, enough said...

Thanks a lot,
Marco

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives