rewinddir#
Reset a directory handle to the start of its listing.
rewinddir sets the current position of DIRHANDLE back to the
beginning of the directory, so that the next readdir call
returns the first entry again. It is the directory-handle counterpart
to seek on a file handle: no re-open, no re-scan of the
parent directory — just a position reset on the open handle.
Synopsis#
rewinddir DIRHANDLE
What you get back#
1 on success, 0 on failure (with $! set). Failure is rare
in practice — the usual cause is a handle that was never opened or
has already been closed. Check the return value if you care about
either case:
rewinddir $dh
or die "rewinddir failed: $!";
Typical use#
You have already walked a directory with readdir and want to
walk it again without paying for another opendir:
opendir my $dh, $path or die "opendir $path: $!";
my @all = readdir $dh; # first pass: collect everything
rewinddir $dh; # back to the top
while (my $entry = readdir $dh) { # second pass: process one by one
next if $entry =~ /^\.\.?\z/;
process("$path/$entry");
}
closedir $dh;
A second common pattern is reusing one handle across several scans with different filters, where re-opening would be wasteful or would race against concurrent directory changes:
opendir my $dh, $path or die $!;
my @perl = grep { /\.p[lm]\z/ } readdir $dh;
rewinddir $dh;
my @text = grep { /\.txt\z/ } readdir $dh;
closedir $dh;
Edge cases#
Unopened or closed handle: returns
0and sets$!. Underuse warningsyou also get arewinddir() attempted on invalid dirhandlewarning.Mid-scan directory changes:
rewinddiris not guaranteed to surface entries created (or hide entries removed) afteropendirreturned. The underlyingrewinddir(3)operates on the kernel’s cached directory stream; whether a rewound scan observes concurrentmkdir,unlink, orrenamein the same directory is filesystem- and OS-dependent. If you need a fresh snapshot,closedirandopendiragain.Not a seek to an arbitrary position:
rewinddirresets to the start and nothing else. To return to a remembered mid-directory position usetelldir/seekdirinstead.Entry order is unspecified: rewinding replays the same stream the kernel gave you, but the order of entries is whatever the filesystem produces — not sorted, not creation order, not the order you saw last time on a different handle. Sort explicitly if order matters.
Portability:
rewinddiris a POSIX call. On platforms without a workingrewinddir(3)it may be implemented by closing and re-opening the directory; see L<perlport/rewinddir> for the historical list. On Linux (the platform pperl targets) this is not a concern.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
opendir— open a directory and obtain the handle thatrewinddiroperates onreaddir— read the next entry; resumes from whereverrewinddirleft the stream (the beginning)closedir— release the handle; pair withopendirwhen a rewound scan is not enough and you need a fresh snapshot of the directorytelldir— record the current position so you can return to it laterseekdir— restore a position previously captured bytelldir;rewinddir $dhis equivalent toseekdir $dh, 0on conforming systems, butrewinddiris the portable spelling