Attention: If you use this module for the specified Berkeley DB file, please use this module for all access to the bdb. By it, you can control lock to the bdb file. Lock files are created under /tmp/bdb_home. If you set ram 1 in new option, lock files are created under /dev/shm/bdb_home.
my $pro=new test_bdb; $pro->run();
sub new(){ my $self={}; return bless $self; }
sub run(){ my $self=shift; $self->init_vars(); $self->demo(); }
sub init_vars(){ my $self=shift; $self->{'bdb'}='/tmp/test.bdb'; $self->{'bdbw'}=new BDB::Wrapper; }
sub demo(){ my $self=shift; # Open db handler for writing if(my $dbh=$self->{'bdbw'}->create_write_dbh($self->{'bdb'})){ # Ignore Ctr+C while putting to avoid to destruct bdb. local $SIG{'INT'}='IGNORE'; local $SIG{'TERM'}='IGNORE'; local $SIG{'QUIT'}='IGNORE'; # put if($dbh->db_put('name', 'value')==0){ } else{ die 'Failed to put to '.$self->{'bdb'}; } $dbh->db_close(); }
# Open db handler for reading if(my $dbh=$self->{'bdbw'}->create_read_dbh($self->{'bdb'})){ my $value; # read if($dbh->db_get('name', $value)==0){ print 'Name='.$name.' value='.$value."\n"; } $dbh->db_close(); } }
To install BDB::Wrapper, copy and paste the appropriate command in to your terminal.
cpanm
cpanm BDB::Wrapper
CPAN shell
perl -MCPAN -e shell install BDB::Wrapper
For more information on module installation, please visit the detailed CPAN module installation guide.