Why does Perl's readdir() cache directory entries?

Posted by Frank Straetz on Stack Overflow See other posts from Stack Overflow or by Frank Straetz
Published on 2010-05-26T10:12:01Z Indexed on 2010/05/26 14:21 UTC
Read the original article Hit count: 572

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about perl

Related posts about file