I have the following code, which replaces a string with a part of it using $1:
my $string = "abcd1234";
print "$string\n";
$string =~ s/c(d1)2/<found: $1>/;
print "$string\n";
Output:
abcd1234
ab<found: d1>34
Now I want to have variables, which contain the condition and the replacement. But if I do it like this, an error occurs:
my $string = "abcd1234";
print "$string\n";
my $cond = qr/c(d1)2/;
my $rule = "<found: $1>";
$string =~ s/$cond/$rule/;
print "$string\n";
Output:
Use of uninitialized value $1 in concatenation (.) or string
abcd1234
ab<found: >34
I get, that $1 isn't existing in the line, where $rule is defined. But how can put a placeholder there?