> I see two ways of writing this code.

Hmmm, how about a third way.  The following code dynamically loads a module
based on the extension of the file.  If it successfully loads the module it
instantiates a new object and calls the handle_file() method of the object.

This accomplishes a few things:

1. Allows each file type to be handled by a seperate module.  This allows
you to plug-in new file types as needed without modifying other working
code.  This should make debugging easier.

2. The modularity of this approad simplifies maintenance since each module
is smaller than if you lumped all the code into one place.

3. Each object instantiated to do something with a file can store state if
you ever need it.

# tested code

use strict;
use lib './testlib';
use Error qw(:try);

my @files = ('foobar.log');

foreach my $file (@files) {
  if ($file =~ /.+\.(\w+)$/) {
    my $ext = $1;

    try {
      require "MyWorkers/$ext.pm";

      my $obj = "MyWorkers::$ext"->new();
      $obj->handle_file($file);

    }
    catch Error::Simple with {
      warn "Unhandled extension: $ext\n";
    }
  }
}

...Elsewhere in ./testlib/MyWorkers/log.pm

package MyWorkers::log;

sub new
{
  my $class = shift;
  bless {}, $class;
}


sub handle_file
{
  my $self = shift;
  my $file = shift;
  print "Handling $file\n";
  print "Write file handling code here\n";
}

1;


-----Original Message-----
From: NIPP, SCOTT V (SBCSI) [mailto:[EMAIL PROTECTED]
Sent: Thursday, September 25, 2003 10:27 AM
To: [EMAIL PROTECTED]
Subject: Basic question...


        I posted the other day a question about writing a daemon to monitor
a directory and then process the files as they arrive in said directory.  I
will begin working on this shortly, but I have a related question and this I
think is mainly a question of good coding practice.  Eventually, there will
be several different types of files that will be hitting this directory for
processing.  The processing that occurs on each file will be based on the
filename.  I see two ways of writing this code.  First, I could write each
section of code as a separate Perl script that is called by the daemon.  The
other way I see doing this is to write each section of code as a subroutine
within the daemon program and have that called.  Any suggestions on which of
these would be better?  More correct?  Etc.?
        Thanks in advance for the help yet again.

Scott Nipp
Phone:  (214) 858-1289
E-mail:  [EMAIL PROTECTED]
Web:  http:\\ldsa.sbcld.sbc.com



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to