Skip to content

Commit

Permalink
Fix GH abw#322: list.grep('0') never returns matches
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed Aug 6, 2024
1 parent d552363 commit ed55d6b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/Template/Manual/VMethods.pod
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ pattern.
...
[% END %]
If the pattern is C<undef> or an empty string, no items are returned.
=head2 sort, nsort
Returns the items in alpha (C<sort>) or numerical (C<nsort>) order.
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/VMethods.pm
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ sub list_reverse {

sub list_grep {
my ($list, $pattern) = @_;
$pattern ||= '';
$pattern = '' unless defined $pattern;
return [ grep /$pattern/, @$list ];
}

Expand Down
33 changes: 33 additions & 0 deletions t/list.t
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ my $vars = {
{ name => 'flute', url => '/blow.html' },
{ name => 'organ', url => '/tulips.html' }, ],
nest => [ [ 3, 1, 4 ], [ 2, [ 7, 1, 8 ] ] ],
ids => [qw( 2112 5150 0 90125 )],
};

my $config = {};
Expand Down Expand Up @@ -200,3 +201,35 @@ romeo, sierra, foxtrot
[% data.grep('^r').join(', ') %]
-- expect --
romeo
-- test --
[% data.grep('XX').join(', ') %]
-- expect --
-- test --
[% data.grep('').join(', ') %]
-- expect --
-- test --
[% data.grep(undef).join(', ') %]
-- expect --
-- test --
[% ids.grep('2').join(', ') %]
-- expect --
2112, 90125
-- test --
[% ids.grep('7').join(', ') %]
-- expect --
-- test --
[% ids.grep('0').join(', ') %]
-- expect --
5150, 0, 90125
-- test --
[% ids.grep('').join(', ') %]
-- expect --
-- test --
[% ids.grep(undef).join(', ') %]
-- expect --

0 comments on commit ed55d6b

Please sign in to comment.