Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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.

HTH!

--
hiseldl
What time is it? It's Camel Time!


Comment on Re: use SDBM_File like a database
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://312016]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (16)
As of 2015-07-02 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (45 votes), past polls