in reply to setting perl ENV from file

Is there anyway to do it from reading and parsing the file?

Replies are listed 'Best First'.
Re: Re: setting perl ENV from file
by halley (Prior) on Sep 16, 2003 at 17:05 UTC
    Assuming you have gotten $key and $value from the file, you could:
    $value =~ s((\$\{(\w+)\}|\$(\w+))) ($ENV{$2} or $ENV{$3} or $1)eg;
    This would support an input file with two possible interpolations:
    key1=value key2=value$key1 key3=${key1}value
    It depends on the order of definition. You could iterate the replacement step across all strings until the keys stop changing, but that may raise new problems.

    --
    [ e d @ h a l l e y . c c ]

Re: Re: setting perl ENV from file
by flounder99 (Friar) on Sep 16, 2003 at 17:09 UTC
    This works for simple cases but is not very robust.
    use Data::Dumper; my %envron; while (<DATA>) { chomp; my ($key, $value) = /(.+)=(.*)/; next unless defined $key; $value =~ s/\$(\w+)/$environ{$1}/g; $environ{$key} = $value; } print Data::Dumper->Dump([\%environ], ["*environ"]); __DATA__ myfile=test.log mydir=/kelly/$myfile longdir=/$myfile$mydir __OUTPUT__ %environ = ( 'longdir' => '/test.log/kelly/test.log', 'mydir' => '/kelly/test.log', 'myfile' => 'test.log' );

    --

    flounder