First, please wrap code selections in [code][/code] tags. Second, if you're working on a Linux system, you should check out one of the inotify modules on CPAN. They give you access to the kernel's event system. A reasonably full-featured option looks to be
Linux::Inotify2, but I haven't worked with it.