Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl tie hash question

Reply
Thread Tools

Perl tie hash question

 
 
Eric
Guest
Posts: n/a
 
      03-07-2007
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:B_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;
}

 
Reply With Quote
 
 
 
 
DJ Stunks
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 1:21 pm, "Eric" <(E-Mail Removed)> wrote:
> 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:B_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

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      03-07-2007
>>>>> "E" == Eric <(E-Mail Removed)> writes:

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

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      03-08-2007
On Mar 7, 1:45 pm, "DJ Stunks" <(E-Mail Removed)> wrote:
> On Mar 7, 1:21 pm, "Eric" <(E-Mail Removed)> wrote:


Thanks for your reply, DJ. My responses inline:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Dumping Perl tie hash file Eric Perl Misc 6 03-10-2007 06:37 PM
tie() with DB_File not tie()ing ? botfood Perl Misc 23 04-26-2006 01:59 AM
Tie a file to a hash SpecialK Perl 1 02-19-2004 07:04 PM
how do i tie a reference to a hash pillbug Perl 2 02-16-2004 02:53 PM



Advertisments