DBIx::Connect::FromConfig - Creates a DB connection from a configuration file
Version 0.07
use DBI; use DBIx::Connect::FromConfig -in_dbi; my $dbh = DBI->connect_from_config(config => $config); # do DBI stuff
or, if you don't want to pollute DBI namespace:
DBI
use DBI; use DBIx::Connect::FromConfig; my $dbh = DBIx::Connect::FromConfig->connect(config => $config); # do DBI stuff
DBIx::Connect::FromConfig provides a generic way to connect to a database using settings from a configuration object.
DBIx::Connect::FromConfig
This module does not export any function, but if given the -in_dbi import option, it will install an alias of the connect() function in the DBI namespace, thus allowing it to be called as a method of DBI (see the synopsis).
-in_dbi
connect()
Try to connect to a database using DBI and return the corresponding object.
Settings
driver - the name of the DBI driver for the database. This parameter is mandatory.
driver
database the name of the database. This parameter is mandatory.
database
host - the hostname of the database. May be empty.
host
port - the port of the database. May be empty.
port
options - DBD options, given as a plain string. Will be appended at the end of the constructed DSN.
options
DBD
username - the user name used to connect to the database. Defaults to the current user.
username
password - the password used to connect to the database. May be empty.
password
attributes - DBI attributes, like RaiseError or AutoCommit.
attributes
RaiseError
AutoCommit
Parameters
config - expects something that contains the settings:
config
a hash reference with the settings stored as first-level keys.
a Config::IniFiles object; the settings must be available from the section named as given by the section parameter.
Config::IniFiles
section
a Config::Simple object; the settings must be available from the section named as given by the section parameter.
Config::Simple
a Config::Tiny object; the settings must be available from the section named as given by the section parameter.
Config::Tiny
section - name of the section to look for the database settings; defaults to "database"
"database"
Examples
Connect to a database, passing the settings in a plain hash reference:
my %settings = ( driver => 'Pg', host => 'bigapp-db.society.com', database => 'bigapp', username => 'appuser', password => 'sekr3t', attributes => { AutoCommit => 1, RaiseError => 1 }, ); my $dbh = DBI->connect_from_config(config => \%settings);
Connect to a database, passing the settings from a configuration file:
my $config = Config::IniFiles->new(-file => '/etc/hebex/mail.conf'); my $dbh = DBI->connect_from_config(config => $config);
where the configuration file could look like:
[database] driver = Pg host = bigapp-db.society.com database = bigapp username = appuser password = sekr3t attributes = AutoCommit=1,RaiseError=1
Database driver not specified
(E) The setting specifying the database driver was not found or was empty.
Database driver %s not supported
(E) The specified database driver is not supported by this module.
DBI attributes must be given as a hashref or a string
(E) The function was given an improper value for the DBI attributes.
No parameter given
(E) The function can't do anything if you don't give it the required arguments.
Odd number of arguments
(E) The function expects options as a hash. Getting this message means something's missing.
Unknown type of configuration
(E) The function doesn't know how to handle the type of configuration you gave it. Use a supported one, bug the author, or send a patch ;-)
;-)
Sébastien Aperghis-Tramoni, <sebastien at aperghis.net>
<sebastien at aperghis.net>
Please report any bugs or feature requests to bug-dbix-connect-fromconfig at rt.cpan.org, or through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Connect-FromConfig. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-dbix-connect-fromconfig at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc DBIx::Connect::FromConfig
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Connect-FromConfig
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/DBIx-Connect-FromConfig
CPAN Ratings
http://cpanratings.perl.org/d/DBIx-Connect-FromConfig
Search CPAN
http://search.cpan.org/dist/DBIx-Connect-FromConfig
Copyright 2008-2012 Sébastien Aperghis-Tramoni, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install DBIx::Connect::FromConfig, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Connect::FromConfig
CPAN shell
perl -MCPAN -e shell install DBIx::Connect::FromConfig
For more information on module installation, please visit the detailed CPAN module installation guide.