in reply to Re: Setting environment variables by reading them from a configuration file
in thread Setting environment variables by reading them from a configuration file

Im new to perl.... I guess my earler posting was not clear...
Heres what I tried
test1.pl....
my $value='abcdef';
$ENV{'PLATFORM'}=$value;
###### I belive this should set the environment variable PLATFORM

#just to confirm I called another perl program from with in this so if the environment is set then it should be reflected in the called program.
my $res=`perl c:/temp/test2.pl`;
print"\n result from test2.pl == $res\n";

test2.pl.....
my $val=$ENV{'PLATFORM'};
print "\n value of envir var platform= $val\n";
####### I belive this should now give abcdef

RESULTS:
#when test1.pl was run
result from test2.pl == value of envir var platform= abcdef

#### Thus I belive that the environment variables are actually set with in the life time of perl program test1.pl... please correct me if iam wrong.

########## Now my question ############
I need to do a similar stuff but the problem is that the environment variables which need to be set are available in a configuration file which needs to be read and then set, I tried to use a code as given in my earlier posting; however the problem was that when ever I use

$ENV{"$variable"}="$value"; # nothing seems to be happening

$ENV{'PLATFORM'}="$value" ; # This seem to work

is it not possible to use variables "$variable" inside $Env{ }, should it only be specified a string 'blah' ?? please advice...

Thanks Robby
  • Comment on Re^2: Setting environment variables by reading them from a configuration file

Replies are listed 'Best First'.
Re^3: Setting environment variables by reading them from a configuration file
by Eimi Metamorphoumai (Deacon) on Sep 02, 2004 at 14:55 UTC
    There should be absolutely no difference between
    $ENV{'PLATFORM'}="abcdef";
    and
    $variable="PLATFORM"; $ENV{$variable}="abcdef";
    I'm noticing a lot of ugly style in your code, a lot of unnecessary code, but nothing that should stop it from working. The following works for me.
    use strict; if (@ARGV){ print $ENV{PLATFORM}, $/; exit; } while(<DATA>){ chomp; my ($variable, $value) = split '='; $ENV{$variable}=$value; } system("perl", $0, "test"); __DATA__ PLATFORM=abcdef
    In particular, you don't need to use "$variable", just $variable. You don't need to seek to the begining of a file handle you just opened. You don't need to chomp the components if you already chomped the variable they were split from. But none of that should really cause problems. Are you sure that your code is actually being run? That is, you've got the assignment to %ENV inside an if on your open. Do you have an else telling you if the open failed? Could you add code to print out $variable and $value when you're setting them to make sure they really have what you expect?