Config::DB - DataBase Configuration module


 use Config::DB;
 my %attr   = ();                                     # DBI::connect attributes HASH
 my @params = ( "DBI:...", 'usr', 'pwd', \%attr )     # DBI::connect parameters ARRAY
 my %tables = ( table1 => 'key1', table2 => 'key2' ); # table, key association
 my $cfg    = Config::DB->new( connect => \@params, tables => \%tables );
 my $value1 = $cfg->get( 'table1', 1 );
 my $value2 = $cfg->get( 'table1', 2, 'field2' );
 my $value3 = $cfg->_table2( 3 );
 my $value4 = $cfg->_table2( 4, 'field4' );


This module provides easy ways to make a one shot read of configuration database where tables have an unique key. It requires a DB connection (though a DBI::connect parameter ARRAY) and the list of the tables to read with relative key associated.


new( connect => [ ... ], tables => { ... } )

It creates and returns the Config::DB objects itself gieving it configuration of configuration. The connect parameter must be the reference to an ARRAY which is the DBI::connect parameters ARRAY, the two attributes PrintError and RaiseError are overridden respectively with 0 and 1. The tables parameter must be the reference to an HASH where every key is the name of a table to read and every relative value is its unique key. It dies on error.


It reads all the configuration tables and closes DB connection. It returns no value. This method is iplicitally called on first get call (explicit or by AUTOLOAD). It dies on error, so it is a good idea to call it during application init. It is usefull to call it on a restart or configuration changed event.

get( $table_name, [ $key_value [ , $field_name ] ] )

It returns a configuration table, record or value. Parameter $table_name is the name of the table; parameter $key_value identifies the requested record; parameter $field_name is the name of the field. If parameters $field_name or $key_value are omitted, a Config::DB::Record or a Config::DB::Table are returned. It dies on missing table, missing key value or missing field.


A quicker syntax is offered: following calls are identical...

 my $table1 = $cfg->get( 'table1' );
 my $table1 = $cfg->_table1;

... following calls are identical as well.

 my $rec2 = $cfg->get( 'table2', 2 );
 my $rec2 = $cfg->_table2( 2 );
 my $rec2 = $cfg->_table2->_2;

... following calls are identical as well.

 my $value3 = $cfg->get( 'table3', 3 'field3' );
 my $value3 = $cfg->_table3( 3, 'field3' );
 my $value3 = $cfg->_table3->_3( 'field3' );
 my $value3 = $cfg->_table3->_3->_field3;