Data::Eacherator - simple each-like iterator generator for hashes and arrays
my $iter = eacherator($hash_or_array); while (my ($k, $v) = $iter->()) { # ... }
This module is designed as a simple drop-in replacement for "each" on those occasions when you need to iterate over a hash or an array.
That is, if $data is a hash, and you're happily doing something like:
$data
while (my ($k, $v) = each %$data) { # ... }
but then decide that you also want to loop over $data in the event that it's an array, you can do:
my $iter = eacherator($data); while (my ($k, $v) = $iter->()) { # ... }
(You may wish to use this package if, for example, you have a module that happily iterates over a hash, but then discover that you also need to iterate over an "ordered" hash--in this case you can just switch curly brackets to square brackets and use eacherator() to generate a drop-in replacement for each.)
eacherator()
Returns a function (closure) that behaves like "each".
Not tested; it's probably quite a bit slower than regular "each" on hashes, though.
If you need something more sophisticated, or something with similar--but different--behaviour, try Data::Iter, Data::Iterator, Array::Each or Object::Iterate. (All of these generate iterators (some with more each-like semantics than others), but none are indifferent as to whether they receive a hash or array.)
Data::Iter
Data::Iterator
Array::Each
Object::Iterate
Depending on what you're trying to do, Maptastic may also be useful.
Maptastic
Michael Stillwell <mjs@beebo.org>
To install Data::Eacherator, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Eacherator
CPAN shell
perl -MCPAN -e shell install Data::Eacherator
For more information on module installation, please visit the detailed CPAN module installation guide.