Why aren't the variables being used as variables if it's read from a file?

Because Perl treats text as text unless you specifically tell it to treat it in some other way.

Perl is manipulating the text file in exactly the way you've requested; it's opening the text file, slurping it into an array, and then printing the array which contains the text. That's what you've asked it to do. If you want it to do something with the text, such as interpreting it as code, you'll have to tell Perl to do that too. For that, there is the do command, or require, or eval, to name a few options. None of them should be given carte blanch power to execute text that comes from untrusted sources. But that's another topic...

The point is that Perl can do pretty much what you want, but you're just not asking it to do what you want.

Imagine if Perl's default behavior was to treat a text file as code the instant you read it into memory. What a mess that would be!


Dave


In reply to Re: Reading from txt makes variables literal? by davido
in thread Reading from txt makes variables literal? 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.