Lotus1 has asked for the wisdom of the Perl Monks concerning the following question:
I have been given a set of batch files that use an ini file with environment variables defined like shown below. Every batch file calls a common bat file to read this ini and set local environment variables. I'm rewriting the batch into Perl but I'm trying not to change the configuration too much since folks are used to using it.
I wrote the following script to do environment variable expansion from the variables defined in the ini file. Is there a better way or a Perl Module that will do this? I'm hoping to not end up supporting this forever so I'm trying to use modules that will handle various edge cases I might not think of. This will need to handle things like spaces before and after the variable names which I haven't implemented yet. And I'll have to write something to handle garbled ini files.
use warnings; use strict; foreach(<DATA>){ chomp; # Skip blank or comment lines. next if /^\s*$|^\s*;/; # Trim comments from the end of lines. s/\s*;.*$//; my ($key,$val)=split /\s*=\s*/; $ENV{$key} = $val; $ENV{$key} =~ s/%([^%]+)%/$ENV{$1}/g; print "$key = $ENV{$key}\n"; } __DATA__ ; ;comments PROJECT_HOME=D:\Scripts\Projectname\ ;comments package1=pack_test package1_home=%PROJECT_HOME%%package1%
The output looks like this:
PROJECT_HOME = D:\Scripts\Projectname\ package1 = pack_test package1_home = D:\Scripts\Projectname\pack_test
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Expanding environment variables from ini file
by swl (Parson) on Aug 27, 2019 at 06:27 UTC | |
by Lotus1 (Vicar) on Aug 27, 2019 at 14:07 UTC | |
Re: Expanding environment variables from ini file
by pryrt (Abbot) on Aug 27, 2019 at 13:41 UTC | |
by Lotus1 (Vicar) on Aug 27, 2019 at 15:41 UTC | |
Re: Expanding environment variables from ini file
by LanX (Saint) on Aug 27, 2019 at 10:23 UTC | |
by Corion (Patriarch) on Aug 27, 2019 at 10:27 UTC | |
by haj (Vicar) on Aug 27, 2019 at 11:24 UTC | |
by LanX (Saint) on Aug 27, 2019 at 10:29 UTC | |
by Lotus1 (Vicar) on Aug 27, 2019 at 15:29 UTC |