diff --git a/lib/Template/VMethods.pm b/lib/Template/VMethods.pm index 364019b28..b10a39c8c 100644 --- a/lib/Template/VMethods.pm +++ b/lib/Template/VMethods.pm @@ -220,7 +220,7 @@ sub text_replace { my ($chunk, $start, $end) = @_; $chunk =~ s{ \\(\\|\$) | \$ (\d+) }{ $1 ? $1 - : ($2 > $#$start || $2 == 0) ? '' + : ($2 > $#$start || $2 == 0 || !defined $start->[$2]) ? '' : substr($text, $start->[$2], $end->[$2] - $start->[$2]); }exg; $chunk; diff --git a/t/vmethods/replace.t b/t/vmethods/replace.t index e0f66fc42..2895c0f2b 100644 --- a/t/vmethods/replace.t +++ b/t/vmethods/replace.t @@ -200,4 +200,10 @@ oo\$1 bar -- expect -- f!! ba!r! f!!ba!r! - +-- test -- +-- name: no warnings -- +[% text = 'foo'; + text.replace('(optional)?(foo)', '$1$2'); +%] +-- expect -- +foo