in reply to Get variables from txt file

Substitute variable names with previously read values. This requires V1 to have been defined before V2, etc.:
use warnings; use strict; my %vars; while (<DATA>) { chomp; s/\$(\w+)/exists $vars{$1} ? $vars{$1} : die "Error: no env var $1 +\n"/ge; my ($key, $value) = split(/=/, $_); $vars{$key} = $value; } use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\%vars); __DATA__ V1=/home/user V2=$V1/test V3=$V2/file.txt

Outputs:

$VAR1 = { 'V1' => '/home/user', 'V2' => '/home/user/test', 'V3' => '/home/user/test/file.txt' };

Replies are listed 'Best First'.
Re^2: Get variables from txt file
by Anonymous Monk on Aug 29, 2014 at 08:45 UTC

    Hi,

    I have tried your solution and it works! Probably, the other solutions will work! Thank you for your time.

      There are several modules which will interpolate variables into strings so you needn't roll your own. See String::Interpolate