/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:code:#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
output:#!/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; } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |