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


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to