robby123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
We have a configuration file from which we need to read and set some environment variables

if(open IPFH,"<$fname") { seek IPFH,0,0; while (! eof IPFH) { my $con=<IPFH>; chomp $con; my($variable,$value)=split '=',$con; chomp $variable; chomp $value; $ENV{"$variable"}=$value; }

The issue is that Environment variables are not being set when we do this way; however when we explicitly specify like this $ENV{'HOSTS'}=$value; it works.Is there a way to overcome this.

Thanks in advance Robby

20040902 Edit by Corion: Added formatting

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"; # not thing seems to be happening

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

please advice...

Thanks Robby
  • Comment on Setting environment variables by reading them from a configuration file
  • Download Code

Replies are listed 'Best First'.
Re: Setting environment variables by reading them from a configuration file
by nimdokk (Vicar) on Sep 02, 2004 at 13:20 UTC
    Are you expecting the environment values to still be true after the script has run? If so, this will not work. The values can be set during the script but once it exits, the values disappear. Other people have noted this in your other posting.

    use Data::Dumper; ## First show the current environment print "What does \%ENV look like now:\n"; $Data::Dumper::Purity=1; print Data::Dumper->Dump([\%ENV], ['*ENV']); ## now we manually add an element to the ENV hash $key="JUST_A_TEST"; $value='just_a_value'; $key_1='ANOTHER_SILLY_KEY'; $ENV{'EXAMPLE'}='12345_test'; $ENV{$key}='yes_this_is_a_value'; $ENV{'EXAMPLE4'}=$value; $ENV{$key_1}=$value; ## Show the current environment after setting manually print "What does \%ENV look like now:\n"; $Data::Dumper::Purity=1; print Data::Dumper->Dump([\%ENV], ['*ENV']); ## Get the key/value pairs from DATA open IN, "environment.txt" or die "Cannot open $input. $!"; foreach $pair (<DATA>) { ($key,$value)=split(/=/,$pair); $ENV{$key}=$value; }#close foreach ## First show the current environment print "What does \%ENV look like now:\n"; $Data::Dumper::Purity=1; print Data::Dumper->Dump([\%ENV], ['*ENV']); #the following data is read from DATA but could be put into "environme +nt.txt" and used from there too. __DATA__ EXAMPLE2=test_test_test_1234567890 EXAMPLE3=a_very_modern_major_sample_of_a_has_value
Re: Setting environment variables by reading them from a configuration file
by Crian (Curate) on Sep 02, 2004 at 10:06 UTC
    I can not set environment variables with both ways here:

    my $name = 'BLUBB'; $ENV{$name} = 'foo'; $ENV{'BLAFASEL'} = "bar";


    both does not work here (under win2k) (in the sense, that the values are not seen in set after running that script).

    In any case you don't need to surround the variable with "...".

    Hint: Line breaks in your post are ignored (if they are not in the inner of code tags), you have to use html tags to format it.
Re: Setting environment variables by reading them from a configuration file
by PodMaster (Abbot) on Sep 02, 2004 at 10:08 UTC
    The issue is that Environment variables are not being set when we do this way;
    How do you know? You're examining %ENV, right? Try inserting some debugging statements in the while loop, like
    warn " KEY=$variable VAL=$value ";

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      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
        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?
Re: Setting environment variables by reading them from a configuration file
by Anonymous Monk on Sep 02, 2004 at 11:08 UTC
    I don't know if this will work, but this technique is what we use with Perl DBI and DBD::Oracle so that the proper Oracle environment variables are set:
    #!/usr/bin/perl -w BEGIN { $ENV{'ORACLE_HOME'} = "/path/to/oracle"; $ENV{'ORACLE_SID'} = "abc"; } # the rest of your code goes here
    The message being that statements in the BEGIN block are done before any other statements in your script. Try setting your environment variables in there, and then see if you can see those settings inside your script.

    HTH.

Re: Setting environment variables by reading them from a configuration file
by Anonymous Monk on Sep 02, 2004 at 11:35 UTC
    A trick that I've picked up from somewhere:
    # Put this in the beginning of your program if (@ARGV && $ARGV[0] eq '--environment_set') {shift} else {@ARGV = map {s/'/'"'"'/g; "'$_'"} @ARGV; exec "source '$fname'; exec '$0' --environment_set @ARGV"}