Hello Monks,

Do you know how to interpolate a variable that is read from a file? Below is my situation:

1. I have up to a hundred of simple hashes like this:

my %act = ("food" => "eat it", "drink" => "drink it", "Perl" => "play it");
All the hashes have the same keys. To keep the main source file clean, I leave all these hashes in a single file and require it at the head of the main source file.

2. In the main source file, I use all these hashes in this way:

my $item = "Perl"; my $out = "-p $act1{$item} -p $act2{$item} -p ... ";
but, the $out variable is so long that I want to keep it in another file because there are more than hundred of hashes. I do this by producing a one-line file like this:
-p $act1{$item} -p $act2{$item} ...

then I read this line into $out variable in the main source file. I thought Perl can interpolate the variable with their value. But, it turns out that Perl doesn't do this. The variable which contains the contend read from the file just keep all the variable as it is.

Er, I don't know if I explain this problem clear enough. And could someone help me out of this?


In reply to Interpolation of variables read from a file by phio

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.