[ale] perl question: eval'ing variable as s/// pattern match

Chris Woodfield rekoil at semihuman.com
Sat Jan 12 15:36:39 EST 2008


Hi all,

I'm hoping there's a simple answer to this - I'm trying to get a  
script to use the value of a varable as a pattern replacement, but the  
result always is literal. See the following code:

-------
#!/usr/bin/perl -w

use strict;

my $text = "test";
my $test = '(test)';
my $match = 'matched $1';

print "Testing direct match:\n";
$text =~ s/$test/matched $1/;
print "$text\n";

$text = "test";
print "\nTesting var match:\n";
$text =~ s/$test/$match/;
print "$text\n";
--------

If this works the way I want it to, both tests should return the same  
result "matched test". Instead I get:

Testing direct match:
matched test

Testing var match:
matched $1

What do I need to do to get perl to interpret the value of $match as a  
pattern substitution string, not literally?

Thanks,

-C



More information about the Ale mailing list