For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2007 > help accessing NDBM database files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author help accessing NDBM database files
Richard Fernandez

2007-02-21, 6:59 pm

Hi folks,

I'm working on a script in which I want to access NDBM files directly.
I'm using the unix aliases file(s) for testing, but I intend to use this
code for other projects as well.

I have the following files: aliases, aliases.dir, and aliases.pag.
According to the man page for aliases, these are "ndbm files maintained
by newaliases".

I want to write something to query/update the database files (the .dir
and .pag files?) directly, but I need a push in the right direction.

What I've done so far is just modify the code in the perldoc for
NDBM_File as a test, but it doesn't work. I am at a loss for how to
begin. Any suggestions would be appreciated.

Thanks.

Here's my

<CODE>
#!/usr/bin/perl
use warnings;
use strict;
use NDBM_File;
use Fcntl;


my %ALIAS;

tie(%ALIAS, 'NDBM_File', './aliases', O_RDWR|O_CREAT, 0644) or
die "Couldn't tie NDBM file : $!; aborting";

print "My alias =3D ", $ALIAS{rfernandez}, "\n";

untie %ALIAS;

</CODE>

This produces this output:
Use of uninitialized value in print at ./mytest1.pl line 13.
My alias =3D=20

and creates a file called aliases.db

Thanks!

richf
Tom Phoenix

2007-02-21, 6:59 pm

On 2/21/07, RICHARD FERNANDEZ <rfernandez@arrow.com> wrote:

> I have the following files: aliases, aliases.dir, and aliases.pag.
> According to the man page for aliases, these are "ndbm files maintained
> by newaliases".


That man page might be lying to you. If you have a pair of .dir/.pag
files, those are probably a DBM file, not NDBM. Have you tried
DB_File?

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Richard Fernandez

2007-02-21, 6:59 pm

> That man page might be lying to you. If you have a pair of=20
> .dir/.pag files, those are probably a DBM file, not NDBM.=20
> Have you tried DB_File?
>=20
> Hope this helps!
>=20
> --Tom Phoenix
> Stonehenge Perl Training
>=20


I substitued DB_File for NDBM_File and changed the code appropriately,
but I'm still only able to create/access files with a '.db' extension.
Still no luck accessing my original '.dir' and '.pag' files.

I'm wondering if I should be looking at using DBD::DBM instead. Don't
know much about database access (yet!)...

Anyone have any thoughts on this?

Thanks!

richf
Mumia W.

2007-02-21, 6:59 pm

On 02/21/2007 10:27 AM, RICHARD FERNANDEZ wrote:
> Hi folks,
>
> I'm working on a script in which I want to access NDBM files directly.
> I'm using the unix aliases file(s) for testing, but I intend to use this
> code for other projects as well.
>
> I have the following files: aliases, aliases.dir, and aliases.pag.
> According to the man page for aliases, these are "ndbm files maintained
> by newaliases".
>
> I want to write something to query/update the database files (the .dir
> and .pag files?) directly, but I need a push in the right direction.
>
> What I've done so far is just modify the code in the perldoc for
> NDBM_File as a test, but it doesn't work. I am at a loss for how to
> begin. Any suggestions would be appreciated.
>
> Thanks.
>
> Here's my
>
> <CODE>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use NDBM_File;
> use Fcntl;
>
>
> my %ALIAS;
>
> tie(%ALIAS, 'NDBM_File', './aliases', O_RDWR|O_CREAT, 0644) or
> die "Couldn't tie NDBM file : $!; aborting";
>
> print "My alias = ", $ALIAS{rfernandez}, "\n";
>
> untie %ALIAS;
>
> </CODE>
>
> This produces this output:
> Use of uninitialized value in print at ./mytest1.pl line 13.
> My alias =
>
> and creates a file called aliases.db
>
> Thanks!
>
> richf
>


Probably rfernandez is not a valid key in the database file. Try this:

tie my %db, 'NDBM_File', 'aliases', O_RDWR, 0644;

while (my ($key, $value) = each %db) {
print "$key => $value\n";
}

untie %db;

On my system, this code works with a custom aliases database file I
created in Perl (aliases.dir and aliases.pag). However, I used simple
numbers for the values, but a true aliases file probably uses
binary-packed values.


HTH

Richard Fernandez

2007-02-21, 6:59 pm

> Probably rfernandez is not a valid key in the database file. Try this:
>=20
> tie my %db, 'NDBM_File', 'aliases', O_RDWR, 0644;
>=20
> while (my ($key, $value) =3D each %db) {
> print "$key =3D> $value\n";
> }
>=20
> untie %db;
>=20
> On my system, this code works with a custom aliases database=20
> file I created in Perl (aliases.dir and aliases.pag).=20
> However, I used simple numbers for the values, but a true=20
> aliases file probably uses binary-packed values.
>=20
>=20
> HTH


Thanks Mumia. I had already tried this w/o success.

I removed the O_CREAT switch (per your sample) and started to see "No
such file or directory" when clearly the file was sitting right there. I
also tried providing the path (./aliases). Same error.

Then it occurred to me that maybe I should try the test code on the
"production" box which runs Solaris and perl 5.8.2. My dev box is OBSD
and runs perl 5.8.6.

The test code runs perfectly on the Solaris box! I have no idea what's
going on under the covers. Maybe someone can shed some light on this?

Anyway at least now I know I'm not barking up the wrong tree :)

Oh, and BTW it turns out that the aliases file does not use
binary-packed values as you suggest. It looks to be straight alias =3D>
address(es) pairs as I expected before the subject came up.

Thanks to all!

richf
Mumia W.

2007-02-21, 9:59 pm

On 02/21/2007 03:49 PM, RICHARD FERNANDEZ wrote:
>
> Thanks Mumia. I had already tried this w/o success.
>
> I removed the O_CREAT switch (per your sample) and started to see "No
> such file or directory" when clearly the file was sitting right there. I
> also tried providing the path (./aliases). Same error.
>
> Then it occurred to me that maybe I should try the test code on the
> "production" box which runs Solaris and perl 5.8.2. My dev box is OBSD
> and runs perl 5.8.6.
>
> The test code runs perfectly on the Solaris box! I have no idea what's
> going on under the covers. Maybe someone can shed some light on this?
>
> Anyway at least now I know I'm not barking up the wrong tree :)
>
> Oh, and BTW it turns out that the aliases file does not use
> binary-packed values as you suggest. It looks to be straight alias =>
> address(es) pairs as I expected before the subject came up.
>
> Thanks to all!
>
> richf
>


Load NDBM_File and check what else is loaded. This is what I get:

$ perl -mNDBM_File -le 'print for values %INC'
/usr/share/perl/5.8/warnings/register.pm
/usr/lib/perl/5.8/XSLoader.pm
/usr/share/perl/5.8/Carp.pm
/usr/lib/perl/5.8/NDBM_File.pm
/usr/share/perl/5.8/Exporter.pm
/usr/share/perl/5.8/strict.pm
/usr/share/perl/5.8/Tie/Hash.pm
/usr/share/perl/5.8/warnings.pm

I'm using Perl 5.8.4 and NDBM_File 1.05 on Debian 3.1.

Check the docs for your version of NDBM_File. The maintainers of your
O/S may have made NDBM_File an alias for another DBM module.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com