in reply to Making perl evaluate a variable with a string

What do you expect your code
$a=1; $b=2; .... while ($line = <INFILE>) { chomp($line); # contains $a, $b print "$line\n"; #prints "$a" "$b"
to do? Please provide a sample of the output you're trying to get.

It's not clear what you are trying to achieve. See I know what I mean. Why don't you?

If you read text to print it later, you print text. If you want to replace text (eg. "$a" with "1"), do so with s/\$a/1/ - see perlre.

If you want perl to evaluate the file, say so with eval, do or require. Evaluating a file whilst reading it linewise is tricky and outright impossible if those lines aren't self-contained perl statements (e.g. if the file contains control structures spanning several lines).

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Making perl evaluate a variable with a string
by moked (Beadle) on Apr 01, 2007 at 15:57 UTC
    Hi again I have a template script for another tool. from the template I want generate many scripts with different numerical values (not only replace $a with 1, but also replace in the template script, $a+5 with 6...) So, I have a template script and a variable definintion file which I call with "require" How best to implement? Sure I can do it with =~s/.../ but if I can use perl to make the sub in a native manner ($var) it will be much stronger. Thx, Nir