rpollard

An improved Nimbus::CFG Perl library

Discussion created by rpollard on Dec 5, 2009

Many Perl modules take a reference to a hash at initialisation.  Nimbus::CFG will accept one too but then stores the values you give it away in the __PRIVATE data section.

I expected the passed hasref to give default values, the attached module behaves just the same as Nimbus::CFG except the hashref you pass it is used for default config values.

To use it store it as perllib/Sermon/CFG.pm and use some code like this at the start of your probes ...

use 5.010;
use strict;
use warnings;
use lib qw(../../../perllib );

use Nimbus::API;
use Sermon::CFG;   # if renamed alter the package Sermon::CFG; statement in the .pm

my @suffixlist = qw(.pl .pm);
our $SCRIPTNAME = fileparse( $0, @suffixlist );

# Set up our default config settings then make some shortcuts
my $cfg = {
    setup => {
        debug    => 1,
        loglevel => 2,
        logfile  => "$SCRIPTNAME.log",
        logsize  => 1_000_000,
        sid      => '4.6.5.2',
    },
};
my $setup = $cfg->{setup};       # convenient short cut
my $debug = $setup->{debug};  # convenient short cut
$| = 1 if $debug;                        # output buffering off

# read and update the config or send a warning
if ( -r "$SCRIPTNAME.cfg" ) {
    $cfg = Sermon::CFG->new( "$SCRIPTNAME.cfg", $cfg );
}
else {
    my $supp_key = nimSuppToStr( 0, 0, 0, "diskmon_util/config" );
    nimAlarm( NIML_WARNING, "No readable $SCRIPTNAME.cfg", $setup->{sid}, $supp_key );
}


Outcomes