Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

HI, I am using perl to open and read a directory which have many files and doing some operation using those files. Need to know how readdir behavior if files being continuosly written to this directory and perl program initiated in b/w?

Replies are listed 'Best First'.
Re: Open directory command in perl
by LanX (Saint) on Jan 29, 2015 at 20:27 UTC
Re: Open directory command in perl
by GotToBTru (Prior) on Jan 29, 2015 at 20:08 UTC

    A quick test using readdir in scalar context did find a file created after the opendir command..if it's name comes after the current file being processed in alphabetical order. And even that wasn't consistent; could be a fault of the slapdash test methodology. But based on this, I would not count on it finding newly created files.

    Dum Spiro Spero
Re: Open directory command in perl
by locked_user sundialsvc4 (Abbot) on Jan 29, 2015 at 23:38 UTC

    I would expect the outcomes to be unpredictable.   The directory-scan is, by definition, passing through a highly volatile data structure without the benefit of locks.   The data structure being traversed, in most file systems, is not simply “a list,” but a tree of some kind.   If the directory is updated while a particular scan is going on, the results of that scan might be quite unpredictable ... with omitted entries, possible duplicates, and so on.

    A program which makes any assumptions at all about the outcome of this scenario will, eventually, produce incorrect results ... probably without realizing that it has done so.