$str =~ s{$pat}{ prompt( $_, $-[0], $+[0]-$-[0], $&, $repl, $`, $' ) ? $repl : $& }eg #### use String::Substitution qw( interpolate_match_vars last_match_vars ); $str =~ s{$pat}{ my $true_repl = interpolate_match_vars( $repl, last_match_vars() ); prompt( $_, $-[0], $+[0]-$-[0], $&, $true_repl, $`, $' ) ? $true_repl : $& }eg