Robert's Perl Tutorial

http://www.sthomas.net/roberts-perl-tutorial.htm


ChangeNotify

Another example is Win32::ChangeNotify. As you might expect there are a number of Win32-specific modules, and ChangeNotify is one of them. It waits until a something changes in a directory, then acts. What it waits for and what it does are up to you, for example:

use Win32::ChangeNotify;

$Path='/downloads';
$WatchSubTree=0;
$Events='FILE_NAME';
$browser='E:/progs/netscape/Communicator/program/netscape.exe';
$changes=0;

$notify = Win32::ChangeNotify->new($Path,$WatchSubTree,$Events);

while (1) {
	print "- ",scalar(localtime)," $changes so far to $Path.\n";
	$notify->wait;
	++$changes;
	print "- ",scalar(localtime), " Launching $browser...\n";
	system("$browser $Path");
	$notify->reset;
}


Again, the module is incorporated into the program with use . An object referred to by the variable $notify is created. The parameters passed are the path to be watched, whether we want to watch subtrees, and what sort of events we want to be notified about, in this case only filename changes.

Then, we enter a loop which continues while 1 is true -- which will be forever.

The program pauses when the wait method of the $notify notify object is called. Only when there is a change to the directory, then the rest of the subroutine completes, launching the browser. We have to reset the $notify object.

There is some pretty frightening stuff about objects in the explanation. But you don't actually need to understand anything about objects. Just read the documentation, and experiment.

You can use as many modules as you like in one program. As they are all written with carefully scoped variables you need not worry about programmers using the same variable names in different modules. Now you *really* appreciate scoping!