3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2009 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1432 2009-04-21 02:42:41Z don $
17 db-hash - create a database and query using it
21 db-hash [options] dbname key
22 db-hash --create [options] dbname < key_value.txt
25 --create, -c create dbname
26 --update, -u update dbname, create if it doesn't exist
27 --debug, -d debugging level (Default 0)
28 --help, -h display this help
29 --man, -m display manual
37 Debug verbosity. (Default 0)
41 Display brief usage information.
54 use Fcntl qw/O_RDWR O_RDONLY O_CREAT O_TRUNC/;
55 use MLDBM qw(DB_File Storable);
59 my %options = (debug => 0,
67 'create|c','update|u',
68 'debug|d+','help|h|?','man|m');
70 pod2usage() if $options{help};
71 pod2usage({verbose=>2}) if $options{man};
73 $DEBUG = $options{debug};
77 # push @USAGE_ERRORS,"You must pass something";
80 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
84 my ($db,@keys) = @ARGV;
88 if (not ($options{create} or $options{update})) {
89 tie %t_db, MLDBM => $db, O_RDONLY or
90 die "Unable to open $db for reading: $!";
93 tie %t_db, MLDBM => $db, O_RDWR|O_CREAT|($options{update}?0:O_TRUNC), 0666 or
94 die "Unable to open $db for writing: $!";
97 if ($options{update}) {
98 die "update currently not supported"
100 elsif ($options{create}) {
104 my ($key,@val) = split /\t/;
105 $fast_db{$key} = [make_list($fast_db{$key} // [],@val)];
107 for my $key (keys %fast_db) {
108 $t_db{$key} = $fast_db{$key};
113 print map {"$_\n"} map {make_list($t_db{$_} // [])} @keys;
116 print map {"$_\n"} map {make_list($t_db{$_} // [])} <STDIN>;
121 return map {(ref($_) eq 'ARRAY')?@{$_}:$_} @_;