my $str = 'abcd'; my $pattern = shift; if ($str =~ $pattern) { my $temp = shift; my $repl = eval $temp; say '$repl: ', $repl; say 'old $str: ', $str; $str =~ s/$pattern/$repl/; say 'new $str: ', $str; } --output:-- $ perl 2perl.pl '(ab)' '--$1' Use of uninitialized value $repl in say at 2perl.pl line 39. $repl: old $str: abcd Use of uninitialized value $repl in substitution (s///) at 2perl.pl line 42. new $str: cd