X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDOM%2FTiny%2F_Collection.pm;h=da649dd3e48904886625df5726ed0e2c86193ec7;hb=22cfa6c8dd84a3ae12b06835c686458f014be14f;hp=b115b1364cf27b5993e75a06a530f3e2c6728803;hpb=9a5f1e3f0633370ed08c12af1542a9e38870c84a;p=catagits%2FDOM-Tiny.git diff --git a/lib/DOM/Tiny/_Collection.pm b/lib/DOM/Tiny/_Collection.pm index b115b13..da649dd 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.001'; +use constant REDUCE => ($] >= 5.008009 ? \&List::Util::reduce : \&_reduce); + +our $VERSION = '0.004'; sub new { my $class = shift; @@ -44,7 +46,7 @@ sub grep { } sub join { - join $_[1] // '', map {"$_"} @{$_[0]}; + join +(defined($_[1]) ? $_[1] : ''), map {"$_"} @{$_[0]}; } sub last { shift->[-1] } @@ -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,28 @@ 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"}) = \my $x; + local(*{$caller."::b"}) = \my $y; + + $x = shift; + foreach (@_) { + $y = $_; + $x = $code->(); + } + + $x; +} + sub _ref { ref $_[0] eq 'ARRAY' || blessed $_[0] && $_[0]->isa(__PACKAGE__) } 1;