Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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 } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Daemon To Check For Files
by perrin (Chancellor) on Nov 04, 2003 at 23:06 UTC | |
by Roger (Parson) on Nov 05, 2003 at 01:43 UTC | |
by perrin (Chancellor) on Nov 05, 2003 at 03:26 UTC | |
by Roger (Parson) on Nov 05, 2003 at 03:39 UTC | |
by perrin (Chancellor) on Nov 05, 2003 at 04:20 UTC | |
by Anonymous Monk on Nov 04, 2003 at 23:31 UTC | |
by perrin (Chancellor) on Nov 04, 2003 at 23:49 UTC | |
by EvdB (Deacon) on Nov 05, 2003 at 09:05 UTC | |
|
Re: Daemon To Check For Files
by Roger (Parson) on Nov 05, 2003 at 01:26 UTC | |
|
Re: Daemon To Check For Files
by waswas-fng (Curate) on Nov 04, 2003 at 22:51 UTC | |
by Anonymous Monk on Nov 04, 2003 at 23:42 UTC |