Expanding variables in single-quoted strings.

KevinADC 0 Tallied Votes 272 Views Share

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;