X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-File-KDBX;a=blobdiff_plain;f=lib%2FFile%2FKDBX%2FIterator.pm;h=89c0063f87c766f3eeb9a504b66745df972a3f89;hp=c9fc7612c452fe0318b5f0d9489befd674cbe697;hb=eefcd42a336641c8927b29d12c5c59443212468f;hpb=cec3705c27fbb9fd9bdfead7103480294b673ed1 diff --git a/lib/File/KDBX/Iterator.pm b/lib/File/KDBX/Iterator.pm index c9fc761..89c0063 100644 --- a/lib/File/KDBX/Iterator.pm +++ b/lib/File/KDBX/Iterator.pm @@ -8,7 +8,7 @@ use File::KDBX::Error; use File::KDBX::Util qw(:class :load :search); use Iterator::Simple; use Module::Loaded; -use Ref::Util qw(is_arrayref is_coderef is_scalarref); +use Ref::Util qw(is_arrayref is_coderef is_ref is_scalarref); use namespace::clean; BEGIN { mark_as_loaded('Iterator::Simple::Iterator') } @@ -115,11 +115,19 @@ sub unget { @items = $iterator->each; - $iterator->each(sub($item, $num) { ... }); + $iterator->each(sub($item, $num, @args) { ... }, @args); -Get the rest of the items. There are two forms: Without arguments, C returns a list of the rest of the -items. Or pass a coderef to be called once per item, in order. The item is passed as the first argument to the -given subroutine and is also available as C<$_>. + $iterator->each($method_name, ...); + +Get or act on the rest of the items. There are three forms: + +=for :list +1. Without arguments, C returns a list of the rest of the items. +2. Pass a coderef to be called once per item, in order. Arguments to the coderef are the item itself (also + C<$_>), its index number and then any extra arguments that were passed to C after the coderef. +3. Pass a string that is the name of a method to be called on each object, in order. Any extra arguments + passed to C after the method name are passed through to each method call. This form requires each + item be an object that C the given method. B This method drains the iterator completely, leaving it empty. See L. @@ -129,8 +137,13 @@ sub each { my $self = shift; my $cb = shift or return @{$self->to_array}; - my $count = 0; - $cb->($_, $count++) while defined (local $_ = $self->()); + if (is_coderef($cb)) { + my $count = 0; + $cb->($_, $count++, @_) while defined (local $_ = $self->()); + } + elsif (!is_ref($cb)) { + $_->$cb(@_) while defined (local $_ = $self->()); + } return $self; }