Expanding variables in single-quoted strings. UserPageVisits:181 active 80 80 DaniWeb 561 60 2006-08-28T13:10:39+00:00 https://www.daniweb.com/programming/software-development/code/216774/expanding-variables-in-single-quoted-strings

Expanding variables in single-quoted strings.

KevinADC

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.

181 Views
About the Author
code snippet
my $lamb = 'wolf';
my $text = 'Mary had a little $lamb.';
$text =~ s/(\$\w+)/$1/eeg;
print $text;