Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. Basic Question
      Hello, I've noticed in some sample code that sometimes people use the @ before a string when concatenating them. Example: string filePath =...
    4. 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
    5. 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...
  3. #2

    Default 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

  4. #3

    Default Re: Basic question...

    "NIPP, SCOTT V (SBCSI)" wrote:
    > 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.
    I would strongly recommend against calling other scripts. If you need to
    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139