thejasviv has asked for the wisdom of the Perl Monks concerning the following question:
Hello, This could be more of a Linux/OS question, but thought I would try it here anyway. I have a script which gets a directory handle through "opendir()", and then enters a loop. In each iteration of the loop, it should print all the files which where created in the directory since the last iteration. Here is the code:
opendir(my $dirh, "/somewhere") or die; while(1) { while(my $file = readdir($dirh)) { print("$file\n"); } sleep(1); }
The first iteration works exactly as expected - it prints all the files currently present in the directory. However, the subsequent iterations are not working quite as I expect them to. I touched some files manually in the directory after the first iteration, and was expecting them to show up in the script output. However, that didn't happen: the script didn't get anything in the call to readdir at all. Technically I can work around this problem by opening and closing the directory in each iteration - and then do my own bookkeeping to keep track of which files are new. However, having to read the complete directory in each iteration, even when I knew a lot of those files were not new since the last iteration, was something I didn't like too well. Any help/suggestion is greatly appreciated. Sincerely, Thejasvi V
|
|---|