This is going to eventually deteriorate into a bunch of hacks where you provide ways to escape $ characters, and address other complications such as whitespace , or will grow to a full fledged tokenizing / parsing system. You probably don't want to live with the limitations and mess that the first scenario will become, and probably don't want to invest the time in the second scenario.

Unless you really want to reinvent this wheel because no other wheel out there fills your need well enough, go with a module like Config::General. This distribution provides variable interpolation via Config::General::Interpolated, which is included when you install Config::General. With Config::General, your solution could look like this:

use strict; use warnings; use Data::Dumper; use Config::General; my $c = Config::General->new( -ConfigFile => \*DATA, -InterPolateVars => 1, ); print Dumper {$c->getall()}; __DATA__ V1=/home/user V2=$V1/test V3=$V2/file.txt

Here is the output that will produce:

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

...and then you're done and can move on to the real stuff. ;)


Dave


In reply to Re: Get variables from txt file by davido
in thread Get variables from txt file by Anonymous Monk

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.