Hello all,
I've written a daemon that runs in the background checking for files in a directory. If files are found, I immediately fork. The child will process the file and exit once the file has been processed, the parent goes back to checking the directory for new files and we repeat the cycle again.
In the true spirit of "you don't want to do it like that, you want to do it like this", I'm wondering if anyone can offer any advice as to what they'd do differently and why.
my $dir = "/path-to/some-dir"; while () { opendir(DIR, $dir) || do { # my error code here... }; for my $filefound (grep /\.foo$/, readdir DIR) { my $fullPath = File::Spec->catdir($dir, $filefound); # immediately rename the file my $newPath; ($newPath = $fullPath) =~ s/foo/bar/; rename ($fullPath, $newPath) || do { # my error code here... }; my $kidPid; unless (defined $kidPid = fork()) { # my error code here... } next if $kidPid; # parent continues with dir grep # I'm the child, I process the file then exit } }
In reply to Daemon To Check For Files by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |