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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.