Iterator(3)

πŸ“¦
version 0.4 Β· 4 versions
, in epel 9

🌸 NAME 🌸

okay bestie, this is the Iterator(3) page β€” User Contributed Perl Documentation β€” and the module is Find::File::Iterator - Iterator interface for search files ✨


✨ SYNOPSIS ✨

use File::Find::Iterator;
my $find = File::Find::Iterator->create(dir => ["/home", "/var"],
                                        filter => \&isdir);
sub isdir { -d }

while (my $f = $find->next) { print "file : $f\n" }

#reread with different filter
$find->filter(\&ishtml);
$find->first;
while (my $f = $find->next) { print "file : $f\n" }

sub ishtml { /\.html?$/ }

# using file for storing state
$find->statefile($statefile);
$find->first;
# this time it could crash
while (my $f = $find->next)
{ print "file : $f\n" }

# using imap and igrep
use File::Find::Iterator qw(imap igrep);
my $find = File::Find::Iterator->new(dir => ["/home", "/var"]);
$find = imap { -M } igrep { -d } $find;

πŸ’– DESCRIPTION πŸ’–

Find::File::Iterator is an iterator object for searching through directory trees. You can easily run a filter on each file name, and you can easily save the search state when you want to stop the search and continue the same search later. slay πŸ’…

Find::File::Iterator inherits from Class::Iterator, so you can use the imap and the igrep constructor. 🫢

πŸ› οΈ create(%opt)

This is the constructor. The %opt hash accepts the following keys:

  • dir "=> \@dir" β€” takes a reference to a list of directories.
  • filter "=> \&code" β€” takes a code reference.
  • statefile "=> $file" β€” takes a filename.

πŸ” next

Calling this method makes one iteration. It returns the file name, or "undef" if there is no more work to do. πŸ’€

πŸ₯‡ first

Calling this method makes an initialisation of the iterator. You can use it to do a search again, but with some little change (directory root, statefile option, different filter). ✨

πŸ“ dir([ \@dir ])

This method gets or sets the directory list for the search.

πŸŽ›οΈ filter([ \&code ])

This method gets or sets the filter method used by the next method.

πŸ’Ύ statefile([ $file ])

This method gets or sets the name of the file used to store state of the search (see "STORING STATE").


πŸŽ€ STORING STATE πŸŽ€

If the statefile option of the constructor or the statefile field of the object is set, the iterator uses the Storable module to record its internal state after one iteration and to set its internal state before a new iteration. With this mechanism you can continue your search after an error occurred. iykyk 😭


πŸ¦‹ SEE ALSO πŸ¦‹

  • Class::Iterator

🌈 CREDITS 🌈

Shoutout to Marc Jason Dominius's YAPC::EU 2003 classes. πŸ‘‘


πŸ‘‘ AUTHOR πŸ‘‘

Robert Silve <robert@silve.net>


mwah πŸ’‹ this page is from perl v5.32.1, dated 2022-01-12, and it's the Iterator(3) footer β€” bye bestie πŸŽ€

πŸ“¦ where this page comes from