use PerlTypelessAutovivifyingDB; my $dbh = PerlTypelessAutovivifyingDB->opendb() or die $!; # open master database file, location set on PerlDB module install my $db = $dbh->db('animals'); # use database 'animals', create it if it doesn't exist my $table = $db->table('cats'); # use table 'cats', create it if it doesn't exist $table->key('name'); # alter table to make column 'name' a unique key, permanently my @cats = ( { 'name' => 'lucy', 'age' => 2, 'species' => 'lion' }, { 'name' => 'mary', 'age' => 5, 'species' => 'lion' }, { 'name' => 'cindy', 'age' => 10, 'species' => 'tiger' } ) foreach my $cat (@cats) { $table->set(%$cat); # insert or update entry represented by hash into table. # any non-existant columns are autovivified in the table like in hashes # IF hash assigns a unique key column an existing value # update that entry # ELSE # insert new entry, set any key columns to a new unique value (eg first available integer) } my $sth = $table->select { $a{'species'} eq 'lion' } ('name', 'age'); # prepare and execute select name and age where species is lion; foreach (my ($name, $age) = $sth->fetchrow_array()) { print "$name ($age years), "; # print 'lucy (2 years), mary (5 years)' } $sth->finish(); my $joinedtable = $db->table { $a{'cats.name'} eq $b{'kittens.mothersname'} } ('cats','kittens'); # join two tables on cats.name == kittens.name my $sth2 = $table->select ('cats.name', 'kittens.name'); # default to leftmost table name in column name conflict foreach (my ($name, $kitten) = $sth2->fetchrow_array()) { print "$name has a kitten $kitten. "; } $sth2->finish(); # database automatically closes itself on block exit