Dumping Perl tie hash file

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

  1. Eric

    Eric Guest

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

  2. Eric

    Guest

    "Eric" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Mar 9, 2007
    #2
    1. Advertising

  3. Eric

    Eric Guest

    On Mar 9, 1:59 pm, Michele Dondi <> wrote:
    > On 9 Mar 2007 11:28:20 -0800, "Eric" <> wrote:
    >
    > >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
    Eric, Mar 9, 2007
    #3
  4. Eric

    Eric Guest

    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
    Eric, Mar 10, 2007
    #4
  5. On 2007-03-10 01:17, Eric <> wrote:
    >> 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.


    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


    --
    _ | Peter J. Holzer | Blaming Perl for the inability of programmers
    |_|_) | Sysadmin WSR | to write clearly is like blaming English for
    | | | | the circumlocutions of bureaucrats.
    __/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
    Peter J. Holzer, Mar 10, 2007
    #5
  6. On 9 Mar 2007 14:44:26 -0800, "Eric" <> wrote:

    >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
    --
    {$_=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,
    Michele Dondi, Mar 10, 2007
    #6
  7. Eric

    DJ Stunks Guest

    On Mar 9, 12:28 pm, "Eric" <> wrote:
    > 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
    DJ Stunks, Mar 10, 2007
    #7
    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. SpecialK

    Tie a file to a hash

    SpecialK, Feb 19, 2004, in forum: Perl
    Replies:
    1
    Views:
    461
    Ben Morrow
    Feb 19, 2004
  2. SpecialK

    Tie a large file to a Hash

    SpecialK, Feb 19, 2004, in forum: Perl
    Replies:
    0
    Views:
    446
    SpecialK
    Feb 19, 2004
  3. rp
    Replies:
    1
    Views:
    493
    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:
    441
    botfood
    Apr 26, 2006
  5. Eric

    Perl tie hash question

    Eric, Mar 7, 2007, in forum: Perl Misc
    Replies:
    3
    Views:
    105
Loading...

Share This Page