Thursday, March 3, 2011

How to create a directory watcher in PERL


Most of the time, we want to achieve a REAL-TIME processing. However, there are some limitations that we don't have control over, and one of the best example is without having a direct connection to the application you are processing into.

One way to achieve a REAL-TIME processing without a direct connection is to have a back-end processing that will watch and process transactions. This is what we called directory watcher.

With this post, it will help you create a directory watcher in PERL. Just follow the simple steps below to do that.

1. Set the look up directory where you want your script to look into.

2. Create a NON-ENDING loop using WHILE() syntax. To create a NON-ENDING loop, we just have to pass a variable or condition that will always results to TRUE. Example below pass a variable 1 that results to TRUE. And as best practice, a SLEEP function should be implemented every end of each loop, this is for the script not to eat too much of the memory.

while(1) {
  # this creates a non ending loop coz 1 is equal to true..

  sleep 1;
}

3. Inside your WHILE loop, you have to OPEN the look up directory by using OPENDIR() syntax. And as best practice, you need to close the directory every end of the loop.

opendir(DIR, $lookup_dir) || die "Cannot opendir $lookup_dir: $!";

4. Loop the files we read from a look up directory.

opendir(DIR, $lookup_dir) || die "Cannot opendir $lookup_dir: $!";
while ($file = readdir(DIR))
{
  # file names from a reading directory...
}
closedir DIR;


Hope you like it!! Please see the complete code below for your implementation.

#!/usr/bin/perl

$lookup_dir = "<directory to look into>";

while(1)
{
        print "lookup directory: $lookup_dir\n";

        opendir(DIR, $lookup_dir) || die "Cannot opendir $lookup_dir: $!";
        while ($file = readdir(DIR))
        {
                if (($file ne '.') && ($file ne '..') && ($file !~ /^\./))
                {
                        print "here file: $file\n";
                        # do what you want with the file...
                }
        }

        closedir DIR;
        sleep 1;
}




1;

No comments:

Post a Comment

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.