
Substituting $variable strings in a file
Quote:
> I want to embed variable names in a file
> then read the file and have the variables
> interpreted. Below is the shell of the
> code, I've tried escapes, eval, and read
> all docs I could think of ... I DON'T
> want to do it with S/UBSTI/TUITION or with
> < <HERE. Basically I'm thinking the program
> is compiled and there's no way to get it
> to interpret the $word in the file?
As already mentioned, there is a FAQ for this...
Depending on what can be in the file, this can certainly be tricky.
You need to quote the data twice -- once for the eval, and once so
that it will be treated as a double-quoted string inside the eval.
The problem is that the second quoting mechanism needs to be something
that will not collide with the data.
For example, if the data contains a ", then using:
print eval qq("$string");
will not work as the embedded " will cause a syntax error. You can
reverse it:
print eval "qq($string)";
which will handle embedded "s fine, but will break on embedded ")".
The normal way to get around this problem is with a HERE document, but
you specifically disallowed that. I'm going to assume that you didn't
really mean to disallow it in this circumstance, so here it is:
print eval qq(<<STRING_NOT_IN_THE_DATA
$string
STRING_NOT_IN_THE_DATA);
Watch out for the newline that gets included at the end with this
method. Also note that STRING_NOT_IN_THE_DATA *can* be in the data as
long as it isn't on a line by itself.
--
Ren Maddox