Perl tie hash question

E

Eric

We inherited the block of code below, which is used to register a
specific mount point with a threadsafe hash. The input for this code
is sent from a calling function. Note that this sub is supposed to
output to file $mountsDBFile. The file is created, but is not
readable; it is nothing but cryptic characters. None of us are that
experienced (yet) with hash ties. So I have the following questions:

Based on the code below, is is apparent why the output file is not
readable?

Is the code below even supposed to output into a humanly readable
file?

Since the output file needs to be humanly readable, should we abandon
what we've inherited and start from scratch, and if so, should we
abandon the tie hash approach in favor of something else?

I realize I have a lot to learn about ties, and am in the progress of
doing so.

Thanks in advance to all that respond.

Eric

=======================================

sub RequestMountPoint {
my $self = shift;
my $protocol = shift;
my $bldNum = shift;
my $mntPnt = undef;
my $mountsDBFile = $self->Env->HomeDir()."/mountsDB";

my $mntsDB = {};
unless (open SEMAPHORE, "> /tmp/mounts.lock") {
$self->Env->ReleaseMachines();
die "unexpected problem allocating semaphore";
}
flock SEMAPHORE, Fcntl::LOCK_EX;

tie( %$mntsDB, "MLDBM", $mountsDBFile, O_CREAT|O_RDWR, 0666,
$DB_File::DB_BTREE );

my $mountPoint = $self->Config->MountPointCount();
for (my $label = 0; $label < $mountPoint; $label++){
$mntPnt = "xmnt".$label;
unless (defined($mntsDB->{$mntPnt})){
$mntsDB->{$mntPnt} = {
BldNum => $bldNum,
Protocol => $protocol,
RefCnt => 1,
};
last;
}

if (($mntsDB->{$mntPnt}->{BldNum} == $bldNum) and
($mntsDB->{$mntPnt}->{Protocol} eq $protocol)) {
$mntsDB->{$mntPnt}->{RefCnt}++;
last;
}

$mntPnt = undef;
}
my $ref = $mntsDB->{$mntPnt}->{RefCnt};

untie(%$mntsDB);

close(SEMAPHORE);

return $mntPnt, $ref;
}
 
D

DJ Stunks

We inherited the block of code below, which is used to register a
specific mount point with a threadsafe hash. The input for this code
is sent from a calling function. Note that this sub is supposed to
output to file $mountsDBFile. The file is created, but is not
readable; it is nothing but cryptic characters. None of us are that
experienced (yet) with hash ties. So I have the following questions:

Based on the code below, is is apparent why the output file is not
readable?

Is the code below even supposed to output into a humanly readable
file?

Since the output file needs to be humanly readable, should we abandon
what we've inherited and start from scratch, and if so, should we
abandon the tie hash approach in favor of something else?

I realize I have a lot to learn about ties, and am in the progress of
doing so.

Thanks in advance to all that respond.

Eric

=======================================

sub RequestMountPoint {
my $self = shift;
my $protocol = shift;
my $bldNum = shift;
my $mntPnt = undef;
my $mountsDBFile = $self->Env->HomeDir()."/mountsDB";

my $mntsDB = {};
unless (open SEMAPHORE, "> /tmp/mounts.lock") {
$self->Env->ReleaseMachines();
die "unexpected problem allocating semaphore";
}
flock SEMAPHORE, Fcntl::LOCK_EX;

tie( %$mntsDB, "MLDBM", $mountsDBFile, O_CREAT|O_RDWR, 0666,
$DB_File::DB_BTREE );

<snip>

it's not the tying that's causing you problems - it's the fact that
you're using a DBM file type to store your hash. This creates a
binary file to store the key/value pairs.

if you just want to be able to read your DBM file from the shell there
are tools for this or you could write a quick perl script to do so
(just tie it the same way, and then dump the hash). Or, if you want a
human readable output, I'd suggest using the Config::Simple module,
which will let you tie your hash (for read/write) but the output would
be an .ini style config file.

-jp
 
U

Uri Guttman

E> Based on the code below, is is apparent why the output file is not
E> readable?

E> Since the output file needs to be humanly readable, should we abandon
E> what we've inherited and start from scratch, and if so, should we
E> abandon the tie hash approach in favor of something else?

in general most db type files are not human readable for speed
reasons. where did you get the idea that MLDBM or others would be human
readable? you can use SQLITE on a flat db text file and get the same
behavior but slower and it will be human readable (records are lines
with fields separated by some char).

this has nothing to do with tie but with the db backend you choose (and
it isn't called mounting which is about file systems so i would change
that name).

uri
 
E

Eric

if you just want to be able to read your DBM file from the shell there
are tools for this or you could write a quick perl script to do so
(just tie it the same way, and then dump the hash).

What I want to do is create a humanly readable output file, not read
from STDOUT (if that's what you meant when you said "shell"). Are you
saying that I can/should write a separate Perl script to do this as
post processing (on the file after execution of the script has
completed), or is there a way to write into the file during the
execution of the script in a way that can be read using Dumper, etc.?
Or, if you want a
human readable output, I'd suggest using the Config::Simple module,
which will let you tie your hash (for read/write) but the output would
be an .ini style config file.

I'm looking into this. I am working in a shared environment where I
have to get the IT folks to install any CPAN modules that I need.

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top