Dumping Perl tie hash file

E

Eric

Hello,

This is a repost of sorts. The code below is putting the info in a
file, $mountsDBFile, but it's doing so in binary. The file has to be
humanly readable.

I got a couple of responses on this. Someone responded and said that
there are tools to translate the file, or I can write a Perl script to
run on the file. (I'm assuming they meant post processing of the
file.) I made desperate attempts to do the latter, but with no
success.

Does anyone know of any tools to do this, or how I would write a
script to interpret this file in humanly readable text? Again, I tried
everything I could come up with (too numerable to list here). I'm
afraid we don't have the time to rewrite the way Perl is dealing with
the database to write the info to the file in readable form.

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;
 
X

xhoster

Eric said:
Hello,

This is a repost of sorts. The code below is putting the info in a
file, $mountsDBFile, but it's doing so in binary. The file has to be
humanly readable.

Do you need to preserve the information in the existing file, or can you
just change the script to use a human readable format?

I got a couple of responses on this. Someone responded and said that
there are tools to translate the file, or I can write a Perl script to
run on the file. (I'm assuming they meant post processing of the
file.) I made desperate attempts to do the latter, but with no
success.

First come up with a method that works to tie your hash to a human
readable file starting clean. Only once you have that working should you
worry about porting the already-existing binary file into the new format
that you will be using going forward.

Does anyone know of any tools to do this, or how I would write a
script to interpret this file in humanly readable text? Again, I tried
everything I could come up with (too numerable to list here).

Tell us about two of them. Tell us in detail what problem you encountered.


Xho
 
E

Eric

Does anyone know of any tools to do this, or how I would write a
script to interpret this file in humanly readable text? Again, I tried

Have you tried Data::Dumper, XML::Simple or YAML::Syck? How readable
is human readable enough for you?

Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Thanks for your response, Michele. I have in fact tried Data::Dumper,
but with limited success. Not sure what XML::Simple could do for me
here. I've never heard of YAML::Syck, but I'll look into it.

Eric
 
E

Eric

Thanks for your response. My replies inline:
Do you need to preserve the information in the existing file, or can you
just change the script to use a human readable format?

I initially misunderstood the requirements. It turns out that the
database itself needs to be in text format. This is going to take a
lot more effort than I intially thought. Someone pointed out that
MLDBM is not the way to go about accomplishing this, and suggested
trying things such as SQLite. I'm going to have start investigating
this.
First come up with a method that works to tie your hash to a human
readable file starting clean. Only once you have that working should you
worry about porting the already-existing binary file into the new format
that you will be using going forward.

Yes, that seems to be the solution. I just need to work on getting
there.

Eric
 
P

Peter J. Holzer

I initially misunderstood the requirements. It turns out that the
database itself needs to be in text format. This is going to take a
lot more effort than I intially thought. Someone pointed out that
MLDBM is not the way to go about accomplishing this, and suggested
trying things such as SQLite. I'm going to have start investigating
this.

SQLite isn't in "text format", either. No database system designed to
efficiently search and update data of more than a few megabytes uses a
(human editable) text format.

If your data is smaller, your best way is probably to use XML or YAML
(or maybe even CSV if your data is simple enough) and always read and
write the complete file.

If it is larger you may want to consider ways of getting around the text
format requirement, for example by providing import and export routines
to some text format.

hp
 
M

Michele Dondi

Thanks for your response, Michele. I have in fact tried Data::Dumper,
but with limited success. Not sure what XML::Simple could do for me
here. I've never heard of YAML::Syck, but I'll look into it.

AIUI you were not really needing to tie() but just wanting to dump to
human readable format. I may have got that wrong. In fact you "specs"
were somewhat hard to follow.


Michele
 
D

DJ Stunks

Hello,

This is a repost of sorts. The code below is putting the info in a
file, $mountsDBFile, but it's doing so in binary. The file has to be
humanly readable.

The question is what your definition of "humanly readable" is - do you
mean you have to be able to use more/less/cat to view it? If so,
why? If you allow yourself to use some other tool to read the file,
and you get sensible output out the other side then it doesn't matter
if the file itself is "humanly readable" only that this tool can read
it and let you know what the file contains.
I got a couple of responses on this. Someone responded and said that
there are tools to translate the file, or I can write a Perl script to
run on the file. (I'm assuming they meant post processing of the
file.) I made desperate attempts to do the latter, but with no
success.

Does anyone know of any tools to do this, or how I would write a
script to interpret this file in humanly readable text?

I believe that there are binary tools to read/write/edit DBM files
directly. However, like I mentioned before, it's pretty
straightforward to do it in Perl - in fact you're already doing it.
You just need to reverse the process - tie a hash to the file then
print out the hash.

here's a Perl one-liner which does just that:

jpeavy1@localhost:~/etc> /usr/bin/perl -MDB_File -MData::Dumper -e '
tie %_,"DB_File",shift or die $!;
print Dumper \%_
' dbm_file.dat

$VAR1 = {
'key1' => 'A',
'key11' => 'B',
'key111' => 'B',
'key2' => 'A'
};

-jp
 

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,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top