[ad_1]
Use $1
instead.
While backrefs like \1
work in the substition part of a regex, it only works in string context. The $hm{KEY}
is accesses an item in a hash. The KEY part can be a bareword or an expression. In an expression, \1
would be a “reference to a literal scalar with value 1
” which would stringify as SCALAR(0x55776153ecb0)
, not a back-reference as in a string. Instead, we can access the value of captures in the regex with variables like $1
.
But that requires us to capture a part of the regex. I would write it as:
s/(Cat)/$hm{$1}/;
As a rule of thumb, only use backrefs like \1
within a regex pattern. Everywhere else use capture variables like $1
. If you use warnings
, Perl will also tell you that \1 better written as $1
, though it wouldn’t have detected the particular issue in your case as the \1
was still valid syntax, albeit with different meaning.
[ad_2]