NAME
YAML::DBH - instant database connection from config file on disk
SYNOPSIS
use YAML::DBH 'yaml_dbh';
my $dbh = yaml_dbh( '/home/myself/mysql_credentials.conf' );
EXAMPLE 2
my $conf = YAML::LoadFile('./file.conf');
my $dbh = YAML::DBH::yml_dbh($conf);
EXAMPLE 3
my $dbh = YAML::DBH::yml_dbh({
username => 'james', password => 'awefafw', database => 'oof',
});
EXAMPLE 4
my $dbh = YAML::DBH::yml_dbh({
abs_db => './t/sqlite.db',
});
DESCRIPTION
Point and shoot method of getting a database handle with only a yaml configuration file as argument.
This is meant for people learning perl who just want to get up and running. It's the simplest customizable way of getting a database handle with very little code.
This is mostly for mysql- the default driver used. The conf file may also tell to use a sqlite db instead.
SUBS
Are not exported by default.
yaml_dbh()
Argument is abs path to yaml config file. Returns database handle, this is a DBI connect object.
Optionally you may pass it a conf hashref as returned by YAML::LoadFile instead, to scan it for the parameters to open a mysql connect with, and return a database handle.
THE YAML CONFIG FILE
You basically need a text file with various parameters. We need the hostname, the username, the password, and the name of your database to connect to. We allow the names of the parameters to be all kinds of silly things like 'user', 'username', 'uname','dbuser', 'DbUsEr' .. Case insensitive. If your config file lacks hostname, we use 'localhost' by default. You can also specify 'driver', by default it is 'mysql'
In /etc/my.conf
---
username: loomis
host: localhost
driver: mysql
database: stuff
password: stuffsee
Also acceptable:
---
DBUSER: loomis
DBHOST: tetranomicon
DBNAME: margaux
DBPASS: jimmy
Also acceptable:
---
user; james
pass: kumquat
dbname: stuff
Also acceptable:
---
username: jack
password: aweg3hmva
database: akira
Also acceptable to open a sqlite db:
---
abs_sqlite: /path/to/sqlite.db
Or:
--
abs_db: /path/to/sqlite.db
CAVEATS
Tests will fail unless you have mysqld running, see README.
SEE ALSO
YAML DBI DBD::mysql DBD::SQLite
AUTHOR
Leo Charre leocharre at cpan dot org
COPYRIGHT
Copyright (c) 2009 Leo Charre. All rights reserved.
LICENSE
This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".
DISCLAIMER
This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the "GNU General Public License" for more details.