Commits

Toby Inkster committed 1a51052

NodeList->reverse

  • Participants
  • Parent commits 825be71

Comments (0)

Files changed (2)

lib/XML/LibXML/NodeList.pm

     my $sub  = __is_code(CORE::shift);
     local $_;
     my @results = CORE::grep { $sub->($_) } @$self;
+    return unless defined wantarray;
     return wantarray ? @results : (ref $self)->new(@results);
 }
 
     my $self = CORE::shift;
     my $sub  = CORE::shift;
     $self->map($sub);
-    return $self;
+    return wantarray ? @$self : $self;
+}
+
+sub reverse
+{
+    my $self    = CORE::shift;
+    my @results = reverse @$self;
+    return wantarray ? @results : (ref $self)->new(@results);
 }
 
 sub reduce {
 Caveat: Perl's magic C<$a> and C<$b> variables are not available in
 C<$coderef>. Instead the two terms are passed to the coderef as arguments.
 
+=head2 reverse()
+
+Equivalent to perl's reverse function.
+
 =head2 foreach($coderef)
 
 Inspired by perl's foreach loop. Executes the coderef on each item in
 use strict;
 use warnings;
 
-use Test::More tests => 25;
+use Test::More tests => 26;
 
 use XML::LibXML;
 use IO::Handle;
 # TEST
 is($smallest, 1, 'reduce works 2');
 
+my @reverse = $numbers->reverse;
+
+# TEST
+is(join('|',@reverse), '10|9|8|7|6|5|4|3|2|1', 'reverse works');
+
 # modified version of Scalar::Util::PP::refaddr
 # only works with blessed references
 sub blessed_refaddr {