in reply to filesystems & Perl

Let's say you read the directory, but while you're reading, the next file gets erased.
This is called a Race Condition. You can make your own File Lock that stops files from being erased while reading the directory.

This one video comes to mind that can explain it better if you have time to view it...
YAPC::NA 2016 - Locking with Perl (...hard things should be possible)‎ - David Oswald