The Perl Advent Calendar needs more articles for 2022. Submit your idea today!

NAME

Class::ConfigMgr is a base class for implementing a singleton object configuration manager.

SYNOPSIS

 # a basic subclass
 package Foo::ConfigMgr;
 use Class::ConfigMgr;
 @Foo::ConfigMgr::ISA = qw( Class::ConfigMgr );
 sub init {
    my $cfg = shift;
    $cfg->define(Foo,Default=>1); 
    $cfg->define(Bar,Default=>1); 
    $cfg->define(Baz); 
    $cfg->define(Fred); 
 }
 
 # example config file foo.cfg
 Bar 0
 Fred RightSaid
 # Foo 40
 
 # application code
 Foo::ConfigMgr->read_config('foo.cfg') or
    die Foo::ConfigMgr->errstr;
 my $cfg = Foo::ConfigMgr->instance;
 print $cfg->Foo;  # 1 (default. 40 was commented out.)
 print $cfg->Bar;  # 0
 print $cfg->Fred; # RightSaid
 print $cfg->Baz;  # (undefined)
 # print $cfg->Quux; # ERROR!
 

DESCRIPTION

Class::ConfigMgr is a base class for implementing a singleton object configuration manager. This module is based off of the configuration manager found in Movable Type and a limited subset of AppConfig configuration files.

METHODS

read_config($file)

Initializes the configuration manager by reads the configuration file specified by $file. Returns undefined if the configuration file could not be read. Use the errstr to retreive the error message. This method should only be called once and before any use of the instance method.

instance

instance returns a reference to the singleton object that is managing the configuration. As a singleton object, developers should ALWAYS call this method rather the call than new,

define

This method defines which directives are recognized by the application and optionally a default value if the directive is not explicted defined in the configuration file. define is most commonly used within the init method all subclasses must implement.

error

Captures an error message and return undef. Inherited from Class::ErrorHandler.

errstr

Returns the last captured error message set by error. Inherited from Class::ErrorHandler.

SUBCLASSING

Subclassing Class::ConfigMgr is easy and only requires one method, init, to be implemented.

init

All subclasses of Class::ConfigMgr must implement an init method that defines which directives are recognized and any associated default values. This method is automatically called by read_config before the actual configuration file is read. It is passed a reference to the singleton and the return value is ignored. See the example subclass in the SYNOPSIS.

DEPENDENCIES

Class::ErrorHandler

LICENSE

The software is released under the Artistic License. The terms of the Artistic License are described at http://www.perl.com/language/misc/Artistic.html.

AUTHOR & COPYRIGHT

Except where otherwise noted, Class::ConfigMgr is Copyright 2005, Timothy Appnel, tima@cpan.org. All rights reserved.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 86:

=begin without a target?