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

In reply to Expanding environment variables from ini file by Lotus1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.