For some reason Perl keeps caching the directory entries I’m trying to read using readdir:
opendir(SNIPPETS, $dir_snippets); # or die...
while ( my $snippet = readdir(SNIPPETS) )
{ print ">>>".$snippet."\n"; }
closedir(SNIPPETS);
Since my directory contains two files, test.pl and test.man, I’m expecting the following output:
.
..
test.pl
test.man
Unfortunately Perl returns a lot of files that have since vanished, for example because I tried to rename them. After I move test.pl to test.yeah Perl will return the following list:
.
..
test.pl
test.yeah
test.man
What’s the reason for this strange behaviour? The documentation for opendir, readdir and closedir doesn’t mention some sort of caching mechanism. “ls -l” clearly lists only two files.
The result of
opendirseems to be a list of files which were in the directory at the time it was called. If you alter the directory you need to callrewinddir:Gives you
Without the rewinddir you get
Just testing with C, I get the same thing:
Gives the following (after creating the file “banana” with “touch”):