DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   perl (http://www.daniweb.com/code/perl.html)
-   -   Expanding variables in single-quoted strings. (http://www.daniweb.com/code/snippet541.html)

KevinADC perl syntax
Aug 28th, 2006
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.

  1. my $lamb = 'wolf';
  2. my $text = 'Mary had a little $lamb.';
  3. $text =~ s/(\$\w+)/$1/eeg;
  4. print $text;