Permalink
Browse files

Fixed bug RT#68722 so that list.defined('alpha') always returns false

  • Loading branch information...
abw committed Jan 13, 2012
1 parent cb1d793 commit 77120a2971474d01413a672bacac04fbd1e5b5a8
Showing with 14 additions and 5 deletions.
  1. +2 −0 Changes
  2. +4 −2 lib/Template/VMethods.pm
  3. +8 −3 t/vmethods/list.t
View
@@ -17,6 +17,8 @@
* Fixed bug RT#47929 which caused the XS Stash to die mysteriously when
calling code that used string evaluation (e.g. DateTime).
+* Fixed bug RT#68722 so that list.defined('alpha') always returns false.
+
* Added the TRACE_VARS option to keep track of what variables are used
in a template. It's not documented yet. See t/trace_vars.t for an
example of use.
View
@@ -23,7 +23,7 @@ package Template::VMethods;
use strict;
use warnings;
-use Scalar::Util 'blessed';
+use Scalar::Util qw( blessed looks_like_number );
require Template::Stash;
our $VERSION = 2.16;
@@ -418,7 +418,9 @@ sub list_defined {
# return the item requested, or 1 if no argument to
# indicate that the hash itself is defined
my $list = shift;
- return @_ ? defined $list->[$_[0]] : 1;
+ return 1 unless @_; # list.defined is always true
+ return unless looks_like_number $_[0]; # list.defined('bah') is always false
+ return defined $list->[$_[0]]; # list.defined(n)
}
sub list_first {
View
@@ -17,7 +17,7 @@
use strict;
use warnings;
-use lib qw( ./lib ../lib ../../lib );
+use lib qw( ./lib ../lib ../../lib ../../../lib );
use Template::Test;
# make sure we're using the Perl stash
@@ -170,6 +170,13 @@ list def ok
list two ok
list seven ok
+-- test --
+[% list = [1];
+ list.defined('asdf') ? 'asdf is defined' : 'asdf is not defined'
+%]
+-- expect --
+asdf is not defined
+
-- test --
[% FOREACH person = people.sort('id') -%]
[% person.name +%]
@@ -179,7 +186,6 @@ Richard
Larry
Tom
--- start --
-- test --
[% FOREACH obj = names.sort('name') -%]
[% obj.name +%]
@@ -198,7 +204,6 @@ Mark Jones
Peter Jones
Andrew Smith
William Smith
--- stop --
-- test --
[% FOREACH obj = numbers.sort('name') -%]

0 comments on commit 77120a2

Please sign in to comment.