On 1/25/2004 3:02 PM, Jan Eden wrote:
> Hi Stuart,
>
> @testarray gets the content of testmessage.txt, which contains the string '$name'. You cannot manipulate this string by setting the variable $name. You could do:
>
> @testarray =~ s/\$name/$name/g;
>
> which will replace the literal string '$name' using your variable's content.
or more generally
#!perl
use strict;
use warnings;
my $name = "Randy";
while (my $line = <DATA>) {
$line =~ s/[^\\](\$\w+)\W/eval $1/eg;
print $line;
}
__DATA__
My name is $name.
For each line we read in, we look for any occurence of a unescaped
dollar sign (i.e. not preceeded by a backslash) followed by any
characters valid in a variable name up to any non-valid character. We
then substitute that for the current value of that variable within our
program.
Regards,
Randy.
Thread Previous
|
Thread Next