use strict;
use warnings;
my $repl = '$1';
{
print "attempt 1 ";
my $str = "a,A,c,d";
$str =~ s/([a-z]),/$repl/ig;
print "yields: $str\n";
}
{
no strict 'refs';
print "attempt 2 ";
my $str = "a,A,c,d";
$str =~ s/([a-z]),/$$repl/ig;
print "yields: $str\n";
}
{
print "attempt 3 ";
my $str = "a,A,c,d";
$str =~ s/([a-z]),/eval "$repl"/ieg;
print "yields: $str\n";
}
my $code = 'sub {$_[0] =~ s/([a-z]),/'. $repl . '/ig}';
my $re = eval ($code);
if ($@) { print "eval failed: $@" }
{
print "attempt 4 ";
my $str = "a,A,c,d";
$re->($str);
print "yields: $str\n";
}
__END__
attempt 1 yields: $1$1$1d
Use of uninitialized value in substitution (s///) at zwomp.pl line 17.
attempt 2 yields: d
attempt 3 yields: aAcd
attempt 4 yields: aAcd
####
URL:
domain:
match: http://(\w+)\.(\w+)\.
replace: $2
####
apply s/http://(\w+)\.(\w+)\./$2/ to 'URL' to produce 'domain'