use strict; use warnings; use 5.010; my $str = 'abcd'; my $pattern = '(ab)'; if ($str =~ $pattern) { my $repl = "--$1"; say '$repl: ', $repl; say 'old $str: ', $str; $str =~ s/$pattern/$repl/; say 'new $str: ', $str; } --output:-- $repl: --ab old $str: abcd new $str: --abcd #### my $str = 'abcd'; my $pattern = shift; if ($str =~ $pattern) { my $repl = "--$1"; say '$repl: ', $repl; say 'old $str: ', $str; $str =~ s/$pattern/$repl/; say 'new $str: ', $str; } --output:-- $ perl 2perl.pl '(ab)' $repl: --ab old $str: abcd new $str: --abcd #### my $str = 'abcd'; my $pattern = shift; if ($str =~ $pattern) { my $repl = shift; say '$repl: ', $repl; say 'old $str: ', $str; $str =~ s/$pattern/$repl/; say 'new $str: ', $str; } --output:-- $ perl 2perl.pl '(ab)' '--$1' $repl: --$1 old $str: abcd new $str: --$1cd #### 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