Ask a Question related to PERL Beginners, Design and Development.
-
Scott V Nipp #1
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]sn4265@sbc.com[/email]
Web: http:\\ldsa.sbcld.sbc.com
Scott V Nipp Guest
-
Basic FLV question
Can someone please help me. we wish to stream flv files using Flash Media Server 2. we have installed the program however we are at a loss as to how... -
basic 3d question
Hi, Just a qiuck question....I have created a 3d world and exported it to director. I want to add a basic cube shape I create in 3dstudio max to... -
Basic Question
Hello, I've noticed in some sample code that sometimes people use the @ before a string when concatenating them. Example: string filePath =... -
Basic question b/w ASP & ASP .NET
I would like to know what is the basic difference between ASP and ASP .NET VB AND VB .NET ADO and ADO .NET look forward to your responses -
basic css question
In several of my posts lately, I have made it known that I am nto a huge fan of using newer stuff as opposed to older methods of getting things... -
Rob Hanson #2
RE: Basic question...
> 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:sn4265@sbc.com]
Sent: Thursday, September 25, 2003 10:27 AM
To: [email]beginners@perl.org[/email]
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]sn4265@sbc.com[/email]
Web: http:\\ldsa.sbcld.sbc.com
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Rob Hanson Guest
-
R. Joseph Newton #3
Re: Basic question...
"NIPP, SCOTT V (SBCSI)" wrote:
I would strongly recommend against calling other scripts. If you need to> 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.
process details as part of a problem, that is logically a subroutine, so it
seems more appropriate to use a subroutine. If the functionality of the
subroutine has larfer applicability than the current program, then the
subroutine should be in a functional module as a named subroutine.
There is at least one big practical difference between using a module and
calling another script is that a module is brought into the same processing
space as the calling code. Calling a script involves shelling out, and
interaction between the calling code and the subroutine being called is much
more difficult.
Much better to construct solid functions [aka subs], get to know the
parameter-passing rules for Perl, and then call those functions, whether they
are defined in you script or in a module.
Another advantage to susing subroutines is that, when well-named, they allow for
much more elegant communication to yourself or other humans reading. Choose
active clauses, lower case and with underscores replacing spaces, and your code
can read like a story [sort of]
Here is a very powerful function that does not in itself assign a single
variable, print anything, read anything. It delegates, and explains to the
reader/maintainer what is going on, though:
sub create_message_indices {
my $message_info = {};
my $indices = [];
load_core_header_fields($message_info, $indices);
create_Message_ID_index($message_info);
create_To_index($message_info);
create_From_index($message_info);
create_sender_date_index($message_info);
create_received_date_index($message_info);
create_References_index($message_info);
create_Subject_index($message_info);
create_skip_index($indices);
}
The program of which it is part works beautifully. In this paritcular instance,
the functions called are defined in the same file as the calling function [there
are less than ten lines of "script" in the 600-plus line file], or in a separate
module used by the script. I would recommend that you first work with calling
functions within your file, explore the use of both by-value and reference
parameters, as well as return values, and get very comfortable with calling
functions in different ways, before you move on to packaging functionality [or
class definitions, a more sophisticated use of modules] in modules.
Joseph
R. Joseph Newton Guest



Reply With Quote

