Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

How to do a blocking readdir or similar

by bigcreeklinn (Novice)
on Dec 19, 2005 at 03:05 UTC ( #517654=perlquestion: print w/replies, xml ) Need Help??

bigcreeklinn has asked for the wisdom of the Perl Monks concerning the following question:

Thanks all, for your help. As an OLD unix type, I only thought of solving the problem in that domain. With your pointers, I learned just enough Applescript to call perl from a Folder Action. I have only done "date >>/tmp/timestame" so far, but that proves the process. I also will try to load and use the IO::Kqueue package. That looks like just the ticket for a perl-only approach. Again, thanks. --Linn ------------------------------ Greetings. On my Mac OS X machine, I would like to drag a file to a folder and as it is dropped in, to wake up my perl script. My first attempt was to create a named pipe and have my script waiting on an open function. This worked great for something like cat somefile >mynamedpipe but unfortunately, the application I wanted to export from would not write to the pipe. It needed to delete it and write a normal file. So now I am looking at alternatives. It would be easy to modify the script to process some file that has been written or dropped into a directory, but I would like to control it with something more elegant than polling the directory on a sleep timer. I searched somewhat throught CPAN but didn't see anything initially. Any ideas? Thanks.
  • Comment on How to do a blocking readdir or similar

Replies are listed 'Best First'.
Re: How to do a blocking readdir or similar
by BorgCopyeditor (Friar) on Dec 19, 2005 at 03:31 UTC
    Which version of OS X? This is probably a job for so-called "Folder Actions" (10.3 Panther and later) which allow you to fire off a script in response to adding files. Then you'll need a bit of AppleScript (using "on adding folder items") that fires off your perl script (using "do shell script"). A good PerlMonks-ish site to look for this sort of thing is Good luck.

    --Your punctuation skills are insufficient!

Re: How to do a blocking readdir or similar
by merlyn (Sage) on Dec 19, 2005 at 03:12 UTC
    POSIX doesn't provide such a hook, so most unix-like systems also don't have such a hook. Your solution of polling is probably the best portable solution.

    However, if you're smart enough with AppleScript, you can attach an applescript action to a folder drop, which can then launch your Perl program. I'm not that smart, or I'd point you at a ready solution.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      POSIX doesn't provide such a hook, so most unix-like systems also don't have such a hook
      Actually, Linux provides such a hook. Check out SGI::FAM for using the file alteration monitor based on dnotify or Linux::Inotify2 for newer kernels.

        OS X (which the OP was using) shares more with FreeBSD than Linux, so IO::KQueue is probably a more appropriate choice.

        From the README...

        This module is a low level interface to the FreeBSD and Mac OS X kqueue() system call. This is basically equivalent to epoll() on Linux - a high performance poll() replacement.


        SGI::FAM seems like a more compatible and less linux-only solution, and fam should run on Mac Os X (and BSDs of all stripes really, as it can be compiled to run with kqueue

        Update: Maybe that is not such a good recommendation, SGI::FAM depends on deprecated File::PathConvert, has an unregistered dependency on Test::Helper and doesn't find my libfam without help.
        After that, it does seem to work, though, quite well, though only on famd started as a daemon, not running through inetd.

      That's not smartness, just knowledge. :)

      DWIM is Perl's answer to Gödel

      I describe the Mac side of the process in Automatically Import Images into iPhoto. To launch a Perl script, just call it from the AppleScript that the folder action wants.

      It sounds like the poster will have an annoying little synchronization problem though: the AppleScript creates a semaphore somehow, and the sleeping, blocking script has to check for that. Perhaps the two scripts could talk to each other in some other way, though.

      brian d foy <>
      Subscribe to The Perl Review
Re: How to do a blocking readdir or similar
by john_oshea (Priest) on Dec 19, 2005 at 11:48 UTC
Re: How to do a blocking readdir or similar
by aquarium (Curate) on Dec 19, 2005 at 13:03 UTC
    tail --retry -f file_you_are_expecting | perl
    the hardest line to type correctly is: stty erase ^H

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://517654]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-12-04 11:14 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (30 votes). Check out past polls.