From: Harvie Date: Thu, 5 May 2011 15:55:45 +0000 (+0200) Subject: Experiments with PERL and BerkeleyDB X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=2f01d7c3a85d28b78fb703c2eb5fb8f343a3c816;p=mirrors%2FPrograms.git Experiments with PERL and BerkeleyDB --- diff --git a/perl/db_file/.gitignore b/perl/db_file/.gitignore new file mode 100644 index 0000000..98e6ef6 --- /dev/null +++ b/perl/db_file/.gitignore @@ -0,0 +1 @@ +*.db diff --git a/perl/db_file/db.pl b/perl/db_file/db.pl new file mode 100755 index 0000000..7460aa7 --- /dev/null +++ b/perl/db_file/db.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use BerkeleyDB; + +#my ( %h $k $v ); +tie my %h, "BerkeleyDB::Hash", -Filename => '_fruit.db', -Flags => DB_CREATE || die "Cannot open DB!\n"; +#tie my %h, "Tie::Hash::DBD", "dbi:SQLite:dbname=_sqlite.db"; + +# Add a few key/value pairs to the file +$h{"apple"} = "red"; +#$h{"banana"} = "yellow"; +$h{"orange"} = "orange"; +$h{"tomato"} = "red"; + +use JSON; +use Data::Dumper; +#$h{"test"} = to_json({"from" => "to", "lol" => "rofl"}); +print $h{"test"}."\n"; +print Dumper(from_json($h{"test"}))."\n"; + + +# Check for existence of a key +print "Banana Exists\n\n" if $h{"banana"}; + +# Delete a key/value pair. +delete $h{"apple"}; + +# print the contents of the file +while (my ($k, $v) = each %h) { print "$k -> $v\n" } + +untie %h;