File::KDBX::Iterator(3pm) | User Contributed Perl Documentation | File::KDBX::Iterator(3pm) |
File::KDBX::Iterator - KDBX database iterator
version 0.906
my $kdbx = File::KDBX->load('database.kdbx', 'masterpw'); $kdbx->entries ->where(sub { $_->title =~ /bank/i }) ->order_by('title') ->limit(5) ->each(sub { say $_->title; });
A buffered iterator compatible with and expanding upon Iterator::Simple, this provides an easy way to navigate a File::KDBX database. The documentation for Iterator::Simple documents functions and methods supported by this iterator that are not documented here, so consider that additional reading.
This iterator is buffered, meaning it can drain from an iterator subroutine under the hood, storing items temporarily to be accessed later. This allows features like "peek" and "order_by" which might be useful in the context of KDBX databases which are normally pretty small so draining an iterator completely isn't cost-prohibitive in terms of memory usage.
The way this works is that if you call an iterator without arguments, it acts like a normal iterator. If you call it with arguments, however, the arguments are added to the buffer. When called without arguments, the buffer is drained before the iterator function is. Using "unget" is equivalent to calling the iterator with arguments, and "next" is equivalent to calling the iterator without arguments.
\&iterator = File::KDBX::Iterator->new(\&iterator);
Bless an iterator to augment it with buffering plus some useful utility methods.
$item = $iterator->next; # OR equivalently $item = $iterator->(); $item = $iterator->next(\&query);
Get the next item or "undef" if there are no more items. If a query is passed, get the next matching item, discarding any unmatching items before the matching item. Example:
my $item = $iterator->next(sub { $_->label =~ /Gym/ });
$item = $iterator->peek;
Peek at the next item. Returns "undef" if the iterator is empty. This allows you to access the next item without draining it from the iterator. The same item will be returned the next time "next" is called.
# Replace buffer: $iterator->unget(\@items); # OR equivalently $iterator->(\@items); # Unshift onto buffer: $iterator->unget(@items); # OR equivalently $iterator->(@items);
Replace the buffer (first form) or unshift one or more items to the current buffer (second form).
See "Buffer".
@items = $iterator->each; $iterator->each(sub($item, $num, @args) { ... }, @args); $iterator->each($method_name, ...);
Get or act on the rest of the items. This method has three forms:
NOTE: This method drains the iterator completely, leaving it empty. See "CAVEATS".
\&iterator = $iterator->grep(\&query); \&iterator = $iterator->grep(sub($item) { ... });
Get a new iterator draining from an existing iterator but providing only items that pass a test or are matched by a query. In its basic form this method is very much like perl's built-in grep function, except for iterators.
There are many examples of the various forms of this method at "QUERY" in File::KDBX.
\&iterator = $iterator->map(\&code);
Get a new iterator draining from an existing iterator but providing modified items. In its basic form this method is very much like perl's built-in map function, except for iterators.
\&iterator = $iterator->sort_by($field, %options); \&iterator = $iterator->sort_by(\&get_value, %options);
Get a new iterator draining from an existing iterator but providing items sorted by an object field. Sorting is done using Unicode::Collate (if available) or "cmp" to sort alphanumerically. The "\&get_value" subroutine is called once for each item and should return a string value. Options:
NOTE: This method drains the iterator completely and places the sorted items onto the buffer. See "CAVEATS".
Alias for "order_by".
\&iterator = $iterator->nsort_by($field, %options); \&iterator = $iterator->nsort_by(\&get_value, %options);
Get a new iterator draining from an existing iterator but providing items sorted by an object field. Sorting is done numerically using "<=>". The "\&get_value" subroutine or $field accessor is called once for each item and should return a numerical value. Options:
NOTE: This method drains the iterator completely and places the sorted items onto the buffer. See "CAVEATS".
Alias for "norder_by".
\&iterator = $iterator->limit($count);
Get a new iterator draining from an existing iterator but providing only a limited number of items.
"limit" is an alias for "$iterator->head($count)" in Iterator::Simple.
\@array = $iterator->to_array;
Get the rest of the items from an iterator as an arrayref.
NOTE: This method drains the iterator completely, leaving it empty. See "CAVEATS".
$size = $iterator->count;
Count the rest of the items from an iterator.
NOTE: This method drains the iterator completely but restores it to its pre-drained state. See "CAVEATS".
Alias for "count".
Some methods attempt to drain the iterator completely before returning. For obvious reasons, this won't work for infinite iterators because your computer doesn't have infinite memory. This isn't a practical issue with File::KDBX lists which are always finite -- unless you do something weird like force a child group to be its own ancestor -- but I'm noting it here as a potential issue if you use this iterator class for other things (which you probably shouldn't do).
KDBX databases are always fully-loaded into memory anyway, so there's not a significant memory cost to draining an iterator completely.
Please report any bugs or feature requests on the bugtracker website <https://github.com/chazmcgarvey/File-KDBX/issues>
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
Charles McGarvey <ccm@cpan.org>
This software is copyright (c) 2022 by Charles McGarvey.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2022-11-20 | perl v5.36.0 |