| 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use BerkeleyDB; |
| 5 | |
| 6 | #my ( %h $k $v ); |
| 7 | tie my %h, "BerkeleyDB::Hash", -Filename => '_fruit.db', -Flags => DB_CREATE || die "Cannot open DB!\n"; |
| 8 | #tie my %h, "Tie::Hash::DBD", "dbi:SQLite:dbname=_sqlite.db"; |
| 9 | |
| 10 | # Add a few key/value pairs to the file |
| 11 | $h{"apple"} = "red"; |
| 12 | #$h{"banana"} = "yellow"; |
| 13 | $h{"orange"} = "orange"; |
| 14 | $h{"tomato"} = "red"; |
| 15 | |
| 16 | use JSON; |
| 17 | use Data::Dumper; |
| 18 | #$h{"test"} = to_json({"from" => "to", "lol" => "rofl"}); |
| 19 | print $h{"test"}."\n"; |
| 20 | print Dumper(from_json($h{"test"}))."\n"; |
| 21 | |
| 22 | |
| 23 | # Check for existence of a key |
| 24 | print "Banana Exists\n\n" if $h{"banana"}; |
| 25 | |
| 26 | # Delete a key/value pair. |
| 27 | delete $h{"apple"}; |
| 28 | |
| 29 | # print the contents of the file |
| 30 | while (my ($k, $v) = each %h) { print "$k -> $v\n" } |
| 31 | |
| 32 | untie %h; |