The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Config::DB - DataBase Configuration module

SYNOPSIS

 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 );
 $cfg->read;
 my $value1 = $cfg->get( 'table1', 1 );
 my $value2 = $cfg->get( 'table1', 2, 'field2' );
 my $value3 = $cfg->_table2( 3 );
 my $value4 = $cfg->_table2( 4, 'field4' );

DESCRIPTION

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.

METHODS

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.

read

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.

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

It returns a configuration value or a configuration record. Parameter $table_name is the name of the table containing requested value or record; parameter $key_value identifies the requested record; without $field_name parameter a reference to an HASH containing all the record is returned, if provided the method returns the value of that field as a SCALAR. It dies on missing table, missing key value or missing field.

AUTOLOAD

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

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

... following calls are identical as well.

 my $value2 = $cfg->get( 'table2', 2 'field2' );
 my $value2 = $cfg->_table2( 2, 'field2' );

VERSION

0.0.1