Iterator(3)
π¦ Iterator(3) β like, the manual page, but make it make sense π
okay so this is a page from the User Contributed Perl Documentation (yes that's the manual name, it's giving β¨community vibesβ¨, no cap) and it's all about Class::Iterator, which is literally the moment closures decided to put on a little object-shaped outfit. periodt.
β¨ NAME β¨
Class::Iterator β it's an Iterator class, bestie. that's the whole name. iconic of it honestly π
πΈ SYNOPSIS πΈ
here's the vibe, copy-paste ready, character for character because I would never do you like that:
use Class::Iterator;
my $it = Class::Iterator->new(\&closure_generator);
while (my $v = $it->next) { print "value : $v\n" }
# use map like
my $it2 = imap { ...some code with $_...} $it
while (my $v = $it->next) { print "value : $v\n" }
# use grep like
my $it3 = igrep { ...some code with $_...} $it
while (my $v = $it->next) { print "value : $v\n" }
likeβ¦ imap and igrep?? the way that's just map and grep but for iterators π obsessed.
π DESCRIPTION π
so Class::Iterator is a generic iterator object class. that's the official description, I'm not making it up π«Ά. what it does is take a closure and wrap it up into an object interface β it's literally giving "closure but make it OO". main character energy fr fr.
new(\&closure_generator)β this is the constructor π. the argument is a sub that looks like this:
sub closure_generator {
my $private_data;
return sub {
# do something with $private_data
# and return it
return $private_data
} }
like, you hand it a generator sub, and it hands you back an object. slay.
nextβ calling this method makes one iteration. one step of the loop, one littlenextmoment, and you're moving on π«$o = imap { ... } $itβ this is a creator β¨. it creates a new iterator from an existing iterator, in the manner ofmap. same energy asmap, just iterator-flavored.$o = igrep { ... } $itβ also a creator π. it creates a new iterator from an existing iterator, in the manner ofgrep. filtering but make it lazy, iykyk.
π CREDITS π
shoutout time, we love to see it π:
- Marc Jason Dominius's YAPC::EU 2003 classes.
- Ken Olstad
(no, I didn't fix the spelling of anything, I keep the facts exactly as they came, that's the rule bestie π«Ά)
π§ AUTHOR π§
Robert Silve <robert@silve.net> β thank you for your service, king π
reminder: every option, name, path, and line above is verbatim from the original page. the vibes are new, the facts are not. π
πΈ and that's the whole page!! footer says it's from perl v5.32.1, dated 2022-01-10 β so go forth, iterate responsibly, and remember: your closure deserves an object interface too π¦πβ¨
π¦ where this page comes from
- epel 9: perl-Class-Iterator 0.3-29.el9 Β· GPL+ or Artistic
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms