The variable interpolation in a regular expression is similar to the interpolation variable in a string. It does not evaluate the entire string as an expression, as much as is necessary to fill the variable. Try the following:
$a = 3; print "{$a+1}\n";
and you will see what happens in your regular expression. $a is replaced, but +1 remains. And /x{1+1}/ not equivalent to /x{2}/ .
Your approach to using a separate variable is the one I would use. But there are other possibilities, such as
$a = 3; print "{${\($a+1)}}\n"; print "yes\n" if "abbbbc" =~ /ab{${\($a+1)}}c/;
The shell ${\(...)} makes the expression the same for interpolation. But it is ugly. Another version is to do this as an array with @{[...]} as follows:
$a = 3; print "yes\n" if "abbbbc" =~ /ab{@{[$a+1]}}c/;
I think both of them are uglier than
$a = 3; $aplus1 = $a + 1; print "yes\n" if "abbbbc" =~ /ab{$aplus1}c/;
source share