Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Expanding environment variables from ini file by pryrt
in thread 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-19 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found