-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256
Thank you all for your replies. I was able to do what i needed. I changed a bit the conf files syntax: conf file 1: option=value option2=value2 conf file 2: property=propertyValue property2=propertyValue2 property3=${option2} #instead of $OPTS{option2} Read function: sub _read_conf_file{ my $file = shift; open my $FH, '<', $file; while(my $line = <$FH>){ chomp $line; $line =~ s/\#.*//; next if $line =~ /^\s*$/; my( $key, $val ) = split(/=/,$line,2); next if (!defined $val || $val =~ /^\s*$/); $key =~ s/^\s+|\s+$//g; $val =~ s/^\s+|\s+$//g; if ($val=~/\$\{.+?\}/) { $val =~ s/\$\{(.+?)\}/$OPTS{$1}/g; #This is very interesting! :-D } $OPTS{$key}=$val; } close $FH; } Thank you all. On Tue, 3 Nov 2015 19:34:20 -0800 "$Bill" <dbec...@gmail.com> wrote: > Re-sending with CC to OP since it didn't make the group. > > On 11/3/2015 14:03, David Emanuel da Costa Santiago wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > > Hash: SHA256 > > > > > > Hello all. > > > > I'm trying to interpolate a hash value in a string but i got stuck. > > I already tried with eval, without any success... > > > > > > I have two conf files in the form > > conf file 1: > > option=value > > option2=value2 > > > > conf file 2: > > property=propertyValue > > property2=propertyValue2 > > > > I'm loading these files into a hash. But the problem is that i want > > to access, for example on conf file 2, properties from conf file 1: > > > > property3=$HASH{option2} > > > > > > So i'm getting the hash for conf file 2: > > > > %PROPERTIES=(property=>"propertyValue", property2=> > > "propertyValue2", property3=> "$HASH{option2}" ); > > > > but what i want is: > > %PROPERTIES=(property=>"propertyValue", property2=> > > "propertyValue2", property3=> "value2" ); > > > > > > this is how i'm reading the files: > > > > sub _read_conf_file{ > > open my $FH, '<', shift @_; > > while(<$FH>){ > > chomp; > > s/\#.*//; > > /(\S+)\s*=\s*(.+)/; > > next if (!defined $1 || !defined $2); > > $OPTS{$1}=$2; > > } > > close $FH; > > } > > > > Does anybody knows how to do this? > > I would make s simple addition to your syntax to indicate the value > is to be taken from the first hash using that value as key. I > arbitrarily picked '~' to indicate this condition (you could use > whatever unique marker you want including $HASH and replace my '~'. > I avoided trying to eval the $OPTS{} in file 2, but you could play > with that using 2nd version below which accomplishes the same thing > with eval. > > use strict; > use warnings; > use Data::Dumper; > $Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1; > > my $c1 = <<EOD; # file 1 > option=value > option2=value2 > EOD > > my $c2 = <<'EOD'; # file 2 > property=propertyValue > property2=propertyValue2 > property3=~option2 # arbitrary ~ marker > EOD > > my %OPTS = (); > > _read_conf_file ($c1); > _read_conf_file ($c2); > print (Data::Dumper->Dump([\%OPTS], [qw(%OPTS)])); > exit; > > sub _read_conf_file { # modified to handle from vrbls > instead of files my $src = shift; > > # open my $FH, '<', shift @_; > # while (<$FH>) { > foreach (split /\s*\n\s*/, $src) { > > next if /^\s*#.*/ or /^\s*$/; # remove blank and > comment lines > > if (not /(\S+)\s*=\s*(.+)/) { > print "Invalid config file syntax ($_)\n"; next; > } > > my $k = $1 // ''; my $v = $2 // ''; > if (not $k) { print "Missing key ($_)\n"; next; } > > if ($v =~ /^~/) { # if indirect hash assign > $OPTS{$k} = $OPTS{substr $v, 1}; > } else { > $OPTS{$k} = $v; > } > } > # close $FH; > > } > > __END__ > > eval version: > > foreach (split /\s*\n\s*/, $src) { > > next if /^\s*#.*/ or /^\s*$/; # remove blank and > comment lines > > if (not /(\S+)\s*=\s*(.+)/) { > print "Invalid config file syntax ($_)\n"; next; > } > > my $k = $1 // ''; my $v = $2 // ''; > if (not $k) { print "Missing key ($_)\n"; next; } > > $v = eval $v if $v =~ /^\$/; > $OPTS{$k} = $v; > } > -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQEcBAEBCAAGBQJWOkstAAoJEJ/OLjwuDYzKg/AH/AiTYpPupOrV9fqKM8xKDO7Q i/87w3ynwzB6md5y+zmKZqYvoN1r2NocVvvce3nQH9aSdMIwlRfPFyAPUBEaJpnL Nddcg4N8YktMuo77Ptk6TlluG+Ok3pKPC7F9wyZ8Qh1+CWJLxj5N/KzpJ2Osei5W E9ZzTyUgwz/KzU3GVIvUdSV2A51v7QeIhdPYmnqQKZwnfKQ3U9APxZ+a8AuYXHSb 3d+bnVidNpRSPvkpXgLqCHQwphpYspj2MhOg9cYAeOJxcidWiPxWP2cbL8g8EN6Z z5wb4j1SP2iW6/vZkWC2INudRQWktE5FHo8tUJhrxwlRAKYmyVunv/YghTyOhnY= =yX+d -----END PGP SIGNATURE-----