Data::Eacherator - simple each-like iterator generator for hashes and arrays |
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:
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($hash_or_array_ref)
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.)
Depending on what you're trying to do, Maptastic
may also be
useful.
Michael Stillwell <mjs@beebo.org>
Data::Eacherator - simple each-like iterator generator for hashes and arrays |