From 08057b4a1c7ddbd585f87ccf1dd6bc4edc943f9d Mon Sep 17 00:00:00 2001
From: Slaven Rezic <slaven@rezic.de>
Date: Wed, 18 Apr 2012 12:52:48 +0200
Subject: [PATCH] cease warnings in replace vmethod

---
 lib/Template/VMethods.pm |    2 +-
 t/vmethods/replace.t     |    8 +++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/lib/Template/VMethods.pm b/lib/Template/VMethods.pm
index 364019b..b10a39c 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 e0f66fc..2895c0f 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
-- 
1.7.2.5