Portable DBI->connect()

Sébastien Aperghis-Tramoni, sebastien@aperghis.net

DBI->connect()

  • DBI->connect() is complex

  • non-uniform connection strings

    • dbi:Pg:dbname=$dbname

    • dbi:mysql:database=$database

    • dbi:SQLite:dbname=$dbfile

    • dbi:Oracle:sid=$sid

    • dbi:CSV:f_dir=$dir

DBIx::Connect::FromConfig

  • uniform interface

  • configuration file

    • Config::IniFiles, Config::Simple, Config::Tiny

  •     use DBI;
        use DBIx::Connect::FromConfig -in_dbi;
    
        my $config = Config::IniFiles->new(-file => "/etc/myapp.conf");
        my $dbh = DBI->connect_from_config(config => $config);
        # do DBI stuff

DBIx::Connect::FromConfig

  • also works with a hashref

  •     my %settings = (
            driver      => 'Pg', 
            host        => 'myapp.society.com',
            database    => 'myapp', 
            username    => 'appuser',
            password    => 'sekr3t',
        );
    
        my $dbh = DBI->connect_from_config(config => \%settings);

Thanks