Perl tie hash question

Discussion in 'Perl Misc' started by Eric, Mar 7, 2007.

  1. Eric

    Eric Guest

    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;
    }
    Eric, Mar 7, 2007
    #1
    1. Advertising

  2. Eric

    DJ Stunks Guest

    On Mar 7, 1:21 pm, "Eric" <> 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::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
    DJ Stunks, Mar 7, 2007
    #2
    1. Advertising

  3. Eric

    Uri Guttman Guest

    >>>>> "E" == Eric <> 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.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Mar 7, 2007
    #3
  4. Eric

    Eric Guest

    On Mar 7, 1:45 pm, "DJ Stunks" <> wrote:
    > On Mar 7, 1:21 pm, "Eric" <> 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
    Eric, Mar 8, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pillbug
    Replies:
    2
    Views:
    485
  2. SpecialK

    Tie a file to a hash

    SpecialK, Feb 19, 2004, in forum: Perl
    Replies:
    1
    Views:
    467
    Ben Morrow
    Feb 19, 2004
  3. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  4. botfood

    tie() with DB_File not tie()ing ?

    botfood, Apr 24, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    452
    botfood
    Apr 26, 2006
  5. Eric

    Dumping Perl tie hash file

    Eric, Mar 9, 2007, in forum: Perl Misc
    Replies:
    6
    Views:
    169
    DJ Stunks
    Mar 10, 2007
Loading...

Share This Page