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

I'd like a perl job to monitor its own source and take action when those files change. I think i want to answer the following questions:

details

I have what i hope will be a long-running perl daemon that runs in conjunction with a user's X11 session. The daemon is currently is smart enough to be able to respond to a SIGHUP and re-exec itself cleanly without interrupting the user's experience.

The daemon is implemented as a (very simple) user-facing perl script in /usr/bin and a handful of specialized modules in the usual spots (e.g. /usr/share/perl5/...blah.pm on a debian system).

It's possible that the daemon itself (the script in /usr/bin and the specialized modules) will be updated while one or more instances are running. I'd like a running instance to detect when this happens, and decide (maybe prompting the user, though that's irrelevant to this question) to HUP itself.

I'd be happy to get suggestions for other ways of thinking about this problem too. FWIW, my primary concern with this tool is a GNU/Linux-based desktop environment, followed by other free software desktops. But more portable suggestions would be happily entertained.

  • Comment on how can i detect updates to running code?

Replies are listed 'Best First'.
Re: how can i detect updates to running code?
by Corion (Patriarch) on Oct 12, 2010 at 18:01 UTC

    Have a look through perlvar. %INC will contain a list of all the (Perl) files that have been loaded through use or require. If the -M check for such a file returns a negative value, the file was modified after the script was started.

Re: how can i detect updates to running code?
by ikegami (Patriarch) on Oct 12, 2010 at 18:01 UTC

    Between $0 and the values of %INC, you have a list of most of the files. The most notable exception is that it won't include the compiled component of modules.

    Catalyst's server has similar functionality (activated by the -r command line option). Check it out first.

Re: how can i detect updates to running code?
by dasgar (Priest) on Oct 12, 2010 at 18:09 UTC

    Looking at CPAN, there is File::Monitor that sounds like it might work for what you're wanting. Of course, Corion's and ikegami's suggestions might be better than this module.

Re: how can i detect updates to running code?
by Your Mother (Archbishop) on Oct 12, 2010 at 21:13 UTC