Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am newbie to Perl and I am trying my best to write more effective scripts and make it more elegant. Since last week I am working on automating the manual process which runs over the network to achieve certain task.

Before running the main process which will start executing listed commands in configration file one by one am thinking of doing some checks to make sure every thing is in right place to trap possible common failures.

My concept of doing the check is to have a hash which contains check list commands,
my %checkList = ( DB => DbName, configFile=> filename, iniFile=> filename, remoteHOST=>name );
pass this hash to subroutine,
initilaCheck(\%checkList) || die ¨Check failed!¨;

sub initialCheck { # Iterate over the hash and do following, 1: Check config file in defined location -if failed return; 2: Check connection to Database -if failed return; 3: Check the existence of .ini file in defined location. -if failed return 4. Check connection to Remote hoest, - if failed return return 1; }

Now I am wondering if there is any better way than this to make this process more elegant? Please do suggest.
Thanks

Replies are listed 'Best First'.
Re: How to make check process more elegant
by Marshall (Canon) on Aug 24, 2010 at 13:20 UTC
    It sounds like you should concentrate on automating this manual or semi-manual process first!

    Start using your new process and then see what happens. It didn't sound to me that the problem is that the process produces incorrect results, but rather than it can "abend", fail at some point. And you want to predict or stop that from happening.

    I would highly recommend doing some empirical analysis (testing) to see what the issues really are before diving off into writing a lot of code that may or may not do something useful.

    It could very well be that your efforts should be spent on what is called a "kick the tires" test. Meaning run a quickie test that takes one minute, but is highly predictive of whether the 10 hour process is going to fail. Ideally, this is not special code but a super abbreviated version of the "real thing". This test uses the "real software" but the test is organized so that when it passes, it means that the .ini file, the config file, the local db file, the connection with the remote host can be established, etc. In other words, passing this one minute thing is very predictive that the 10 hour test will run.

Re: How to make check process more elegant
by cdarke (Prior) on Aug 24, 2010 at 13:57 UTC
    Assuming that the number of checks you wish to do is actually larger than your example, I would code each check as a separate subroutine and use a dispatch table. For example:
    sub CheckFile { my $filename = shift; ... } sub CheckHost { my $host = shift; ... } sub CheckDB { my $db = shift; ... } ... my %checkList = ( DbName => \&CheckDB, configFile => \&CheckFile, iniFile => \&CheckFile, remoteHOST => \&CheckHost ); while (my($key,$value) = each %checkList) { &$value($key) or die "Check $key failed"; }
    This would enable you to add many more checks, and to factor our those checks in common. You might wish to change the design of the hash if the entities are not unique, or if you wish to do more than one check on each item. You could place the checking subroutines in their own module and reuse across several scripts.
Re: How to make check process more elegant
by JavaFan (Canon) on Aug 24, 2010 at 12:28 UTC
    I would probably split it into three separate subs:
    checkfile "config-file" and checkdatabase "dbname" and checkfile "ini-file" and checkremote "host" or die "Check failed"