Hello.

I have some boilerplate text files with some variables I'd like to substitute. I'd then like to create new files with the appropriate substitutions based on those; for example (one of the boilerplate text files):

Hello $FIRST_NAME. I'm writing on behalf of $COMPANY...

How would I go about using those files to create new files with the substituted values, so the new file would then be

Hello James. I'm writing on behalf of SoftLayer...

The following works

my $outfile = "new.txt"; open (OUTFILE, ">> $outfile") || die "Can't open $outfile\n"; print OUTFILE "\n"; print OUTFILE "Hello $FIRST_NAME.\n"; print OUTFILE "\n"; print OUTFILE "I'm writing on behalf of $COMPANY...";

But I can't figure out how to do the above while keeping the boilerplate text files seperate from the script itself and still printing the substituted variables rather than the literal names of the variables.

Any help would be greatly appreciated.


In reply to Reading from/writing to files with variables substitutions by Qukz

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.