Wednesday, August 7, 2013

Accessing data in cdb file using Perl

See this post on cdb file related.

Never knew this can be so easy, thanks to the contributors, see here.

To install the library using cpan. Note, must use root to perform this as this will need the root access to create new file/directory.


sudo cpan
install CDB_File

Say you have a cdb file created call abc.cdb with a key summer. Here's how you can get the value of the key summer.


#!/usr/bin/perl
use strict;

use CDB_File;

my $cdb = "abc.cdb";

my %hash;

tie %hash,'CDB_File',$cdb or
die("unable to tie to cdb file");
my $value = $hash{'summer'};

print "$value\n";




Please note, if a key is not found, it will return a null value.


Without the CDB_File library, the tie will not work, and will have this error message :


Can't locate object method "TIEHASH" via package "CDB_File"


So, have fun! :)

No comments:

Post a Comment