use 5.020; use warnings; my %h = ( foo => 1, bar => 2 ); $_ = 'foobar'; s/(foo)/$h{'$1'}/; say $_; $_ = 'foobar'; s/(foo)/$h{$1}/; say $_; my $foo = 'foo'; say "$h{'$foo'}bar"; #### Use of uninitialized value $h{"$1"} in substitution iterator at extr.pl line 7. bar 1bar Use of uninitialized value $h{"$foo"} in concatenation (.) or string at extr.pl line 15. bar