SQLiteを使って

$ cpanm DBD::SQLite
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Data::Dumper;
use utf8;
binmode(STDOUT, ":encoding(UTF-8)"); # perlcritic で utf8 よりこっちの方が良いとのこと
binmode(STDIN,  ":encoding(UTF-8)");

exit (main());

sub main
{
        my $data_source = "dbi:SQLite:dbname=:memory:";
        #my $data_source = "dbi:SQLite:dbname=s.db";
        my $user        = "";
        my $passwd      = "";
        my $dbh = DBI->connect($data_source, $user, $passwd, {AutoCommit => 0});

        create_table($dbh);
        $dbh->do("INSERT INTO config (key, val) VALUES('key1', 'val1')");
        $dbh->do("INSERT INTO config (key, val) VALUES('key2', 'val2')");
        dump_table($dbh);

        $dbh->commit;
        $dbh->disconnect;
        return 0;
}

sub dump_table
{
        my $dbh = shift;
        #my $sth = $dbh->prepare("SELECT * FROM config");
        #$sth->execute();
        #while (my @rec = $sth->fetchrow_array) {
        #       print "@rec\n";
        #}
        #$sth->finish; $sth = undef;

        #my $sth = $dbh->prepare(
        #       'SELECT * FROM config'
        #);
        #my $rv = $sth->execute();
        #my ($key, $val) = $sth->fetchrow_array;
        #print "key=$key val=$val\n";

        #my ($key, $val) = $dbh->selectrow_array(
        #       'SELECT * FROM config'
        #);
        #print "key=$key val=$val\n";

        #use Test::More;
        #my $rows = $dbh->selectall_arrayref(
        #       'SELECT * FROM config',
        #       +{ Slice => {} },
        #);
        #print Dumper $rows;

        my $sth = $dbh->prepare(
                'SELECT * FROM config'
        );
        $sth->execute();
        $sth->bind_columns( \my($col1, $col2) );
        while ($sth->fetch) { # alias fetchrow_arrayref
                print "[$col1] [$col2]\n";
        }
}

sub create_table
{
        my $dbh = shift;
        $dbh->do("CREATE TABLE config (key TEXT, val TEXT)");
}
__END__
[key1] [val1]
[key2] [val2]