perlfaq nodetype
faq_monk
<P>
Let's assume that you have a string like:
<P>
<PRE> $text = 'this has a $foo in it and a $bar';
</PRE>
<P>
If those were both global variables, then this would suffice:
<P>
<PRE> $text =~ s/\$(\w+)/${$1}/g;
</PRE>
<P>
But since they are probably lexicals, or at least, they could be, you'd
have to do this:
<P>
<PRE> $text =~ s/(\$\w+)/$1/eeg;
die if $@; # needed on /ee, not /e
</PRE>
<P>
It's probably better in the general case to treat those variables as
entries in some special hash. For example:
<P>
<PRE> %user_defs = (
foo => 23,
bar => 19,
);
$text =~ s/\$(\w+)/$user_defs{$1}/g;
</PRE>
<P>
See also ``How do
<FONT SIZE=-1>I</FONT> expand function calls in a string?'' in this section of the
<FONT SIZE=-1>FAQ.</FONT>
<P>