X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDOM%2FTiny%2F_Collection.pm;h=547946c9f1c7ade78a2a28d93ac9467db01c877b;hb=b1ec524b3b6c75361f548d73cfda0282db607a9d;hp=4c27742e67e450e4d2494f63dd896d3fe968cb2a;hpb=2d9f516571abb22d42ea26ffaf07bda8a29314f0;p=catagits%2FDOM-Tiny.git diff --git a/lib/DOM/Tiny/_Collection.pm b/lib/DOM/Tiny/_Collection.pm index 4c27742..547946c 100644 --- a/lib/DOM/Tiny/_Collection.pm +++ b/lib/DOM/Tiny/_Collection.pm @@ -6,7 +6,9 @@ use Carp 'croak'; use List::Util; use Scalar::Util 'blessed'; -our $VERSION = '0.002'; +use constant REDUCE => ($] >= 5.008009 ? \&List::Util::reduce : \&_reduce); + +our $VERSION = '0.004'; sub new { my $class = shift; @@ -57,7 +59,7 @@ sub map { sub reduce { my $self = shift; @_ = (@_, @$self); - goto &List::Util::reduce; + goto &{REDUCE()}; } sub reverse { $_[0]->new(reverse @{$_[0]}) } @@ -104,6 +106,27 @@ sub _flatten { map { _ref($_) ? _flatten(@$_) : $_ } @_; } +# For perl < 5.8.9 +sub _reduce (&@) { + my $code = shift; + + return shift unless @_ > 1; + + my $caller = caller; + + no strict 'refs'; + + local (*{"${caller}::a"}, *{"${caller}::b"}) = (\my $x, \my $y); + + $x = shift; + foreach my $e (@_) { + $y = $e; + $x = $code->(); + } + + $x; +} + sub _ref { ref $_[0] eq 'ARRAY' || blessed $_[0] && $_[0]->isa(__PACKAGE__) } 1;