NAME
DR::Tarantool::Iterator - iterator/container class for DR::Tarantool
SYNOPSIS
use DR::Tarantool::Iterator;
my $iter = DR::Tarantool::Iterator->new([1, 2, 3]);
my $item0 = $iter->item(0);
my @all = $iter->all;
my $all = $iter->all;
while(my $item = $iter->next) {
do_something_with_item( $item );
}
METHODS
new
Constructor.
Arguments
Array of items.
List of named arguments, that can be:
- item_class
-
Name of class to bless/construct item. If the field is 'ARRAYREF' then the first element of the array is item_class, and the second element is item_constructor.
- item_constructor
-
Name of constructor for item. If the value is undefined and item_class is defined, iterator will bless value instead construct.
If item_constructor is used, constructor method will be receive three arguments: item, item_index and iterator.
my $iter = DR::Tarantool::Iterator->new( [ [1], [2], [3] ], item_class => 'MyClass', item_constructor => 'new' ); my $iter = DR::Tarantool::Iterator->new( # the same [ [1], [2], [3] ], item_class => [ 'MyClass', 'new' ] ); my $item = $iter->item(0); my $item = MyClass->new( [1], 0, $iter ); # the same my $item = $iter->item(2); my $item = MyClass->new( [3], 2, $iter ); # the same
clone
clone iterator object (doesn't clone items). It is usable if You want to have iterator that have the other item_class and (or) item_constructor.
count
returns count of items that are contained in iterator
item
returns one item from iterator by its number (or croaks error for wrong numbers)
get
The same as item method.
exists
Returns true if iterator contains element with noticed index.
my $item = $iter->exists(10) ? $iter->get(10) : somethig_else();
next
returns next element from iterator (or undef if eof).
while(my $item = $iter->next) {
do_something_with( $item );
}
You can ask current element's number by function 'iter'.
iter
returns current iterator index.
reset
resets iterator index, returns previous index value.
all
returns all elements from iterator.
my @list = $iter->all;
my $list_aref = $iter->all;
my @abc_list = map { $_->abc } $iter->all;
my @abc_list = $iter->all('abc'); # the same
my @list = map { [ $_->abc, $_->cde ] } $iter->all;
my @list = $iter->all('abc', 'cde'); # the same
my @list = map { $_->abc + $_->cde } $iter->all;
my @list = $iter->all(sub { $_[0]->abc + $_->cde }); # the same
item_class
set/returns item class. If the value isn't defined, iterator will bless fields into the class (or calls item_constructor in the class if item_constructor is defined
item_constructor
set/returns item constructor. The value can be used only if item_class is defined.
push
push item into iterator.