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


in reply to Expanding environment variables from ini file

Using Config::Tiny, and assuming you don't actually need it to be in %ENV, but you just want to treat them as internal "environment-style" variables, and also want to accept true environment variables

#!/usr/bin/perl -l # in reponse to https://perlmonks.org/?node_id=11105105 use warnings; use strict; use Config::Tiny; my $ini_string = do { local $/; <DATA> }; $ini_string =~ s/(?<=.);.*$//gmx; # Config::Tiny cannot handle p +ostfix comments my $cfg = Config::Tiny->read_string($ini_string); #use Data::Dumper; $Data::Dumper::Indent = 1; #print Dumper $cfg; # the _ key holds the main-category / top-level of your .ini file; # if you have multiple categories, this loop will have to be modifie +d foreach my $key (keys %{$cfg->{_}}) { while( $cfg->{_}{$key} =~ /\%(.*?)\%/g ) { my $var = $1; next unless exists $cfg->{_}{$var} or exists $ENV{$var}; #print "\$var = >>$var<<\n"; my $repl = exists $cfg->{_}{$var} ? $cfg->{_}{$var} : $ENV{$va +r}; $cfg->{_}{$key} =~ s/\%$var\%/$repl/; } } #print Dumper $cfg; print $cfg->write_string(); __DATA__ ;comments PROJECT_HOME=D:\Scripts\Projectname\ ;comments package1=pack_test package1_home=%PROJECT_HOME%%package1% subtemp=%TEMP%\subdir

On mine, that output

PROJECT_HOME=D:\Scripts\Projectname\ package1=pack_test package1_home=D:\Scripts\Projectname\pack_test subtemp=C:\Users\pryrt\AppData\Local\Temp\subdir

update: finished my first sentence; sorry