Start New Discussion within our Software Development Community

I've seen this question (or similar) asked many times on forums:

I have a variable in a file (text) and want to expand it. For example, I have this line in a text file:

Mary had a little $lamb

and I want to be able to expand $lamb but text files are treated as single-quoted strings and perl does not interpolate variables in single-quoted strings.

my $lamb = 'wolf';
my $text = 'Mary had a little $lamb.';
$text =~ s/(\$\w+)/$1/eeg;
print $text;
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.