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

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.

  • Comment on Re: Setting environment variables by reading them from a configuration file
  • Download Code

Replies are listed 'Best First'.
Re^2: Setting environment variables by reading them from a configuration file
by robby123 (Initiate) on Sep 02, 2004 at 11:13 UTC
    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?