http://qs321.pair.com?node_id=11105105

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