Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://312016]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2022-01-17 11:18 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (51 votes). Check out past polls.