Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: use SDBM_File like a database

by hiseldl (Priest)
on Dec 03, 2003 at 21:20 UTC ( #312016=note: print w/replies, xml ) Need Help??

in reply to use SDBM_File like a database

If you can use Storable you can try the following:

#!/usr/local/bin/perl -w use strict; use Fcntl; # For O_RDWR, O_CREAT, etc. use SDBM_File; use Storable qw/freeze thaw/; ... sub insert { my $table_name = shift; my $col_vals = shift; # ref to hash my %h; tie(%h, 'SDBM_File', $table_name, O_WRONLY , 0666) or die "Couldn't tie SDBM file $table_name: $!; aborting"; $h{$col_vals->{REQ}} = freeze $col_vals; untie %h; } sub select_all { my $table_name = shift; my %h; tie(%h, 'SDBM_File', $table_name, O_RDONLY , 0666) or die "Couldn't tie SDBM file $table_name: $!; aborting"; for my $i ( keys %h ) { my $row = thaw $h{$i}; print $i," -> "; while (my ($key,$val) = each %$row) { $val = 'undef' unless $val; print "\t", $key, ' = ', $val, "\n"; } } } ...
this uses the value of the key 'REQ' as the primary key, and then freezes the hash so it can be stored in the SDBM_File. The select_all sub uses thaw to deserialize the hash into a hash ref containing the 'row' of values.


What time is it? It's Camel Time!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://312016]
[ambrus]: "Corion Meh. My bank removed (part of) their CSV download facilities." => ah great. interface changes.
[ambrus]: Is this for work?
[Eily]: Discipulus try looking at $@
[Eily]: or count the parentheses :)
[Discipulus]: stupid me! I had to stay at home and do not code with the flu..
[holli]: bacause it's a syntax error
[Discipulus]: yes sorry forget and forgive me

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-10-24 10:17 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (287 votes). Check out past polls.