semi-OT, but since the Seeker says regexen solved the problem, I suspect a typo in the OP:

/startpattern/ (Ln 9) is NOT the same as /start pattern/ (in #start pattern in Ln 14.

Further, while others have mentioned the explanation for non-interpolation, no-one, OP included, has posted a solution -- so here's one (which omits the -- confusing to me -- script's use of both a file (of unspecified content) and __DATA__:

Data file:
#start pattern my name is $name blahhhhhhh my id is $id blahhhh #Endpattern #newpattern my name is $name blash my id is $id #endofpattern #startpattern my name is $name blaghhh my id is $id #endpattern #start pattern my name is fred blahhhhhhh my id is 42 blahhhh #Endpattern #1064933.txt
code:
#!/usr/bin/perl use 5.016; use warnings; # 1064933 my $name="guest"; my $id = 1; $/ = "\n\n"; open(SOURCE, "<1064933.txt") or die "Can't open 1064933.txt, $!"; open(SINK, ">1064933OUT.txt") or die "Can't open 1064933OUT.txt, $!"; while (<SOURCE>) { if ( /start pattern/ .. /Endpattern/) { s/\$name/$name/; s/\$id/$id/; print SINK $_; next; } }
output:
D:\_Perl_\PMonks>cat 1064933OUT.txt #start pattern my name is guest blahhhhhhh my id is 1 blahhhh #Endpattern #start pattern my name is fred blahhhhhhh my id is 42 blahhhh #Endpattern

Note, however, that while this limits the exploitability of using an eval solution (by restricting the substitutions to the programmer's specs), it's far short of production-worthy. But then, the notion of globally replacing \$name with "guest" and making all \$id evaluate to 1 suggests that the example code has little resemblence to the actual task at hand.


In reply to Re: variable interpretation while reading from a file by ww
in thread variable interpretation while reading from a file by amma

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.