Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Motional Monks,

For depressing-to-explain reasons, we're using an old DBM file (data_dbm_file) as a backup to a small sql database (data_db) of name/value pairs. Each time data_db is updated, the following sub routine is run:

tie %data_hash, "SDBM_File", "data_dbm_file", O_RDWR|O_CREAT, 0666 + || die "Cannot open file 'data_dbm_file': $!\n"; %data_hash = (); $sqlSelect = "SELECT field1, field2 FROM $data_db"; $sth = $dbh->prepare($sqlSelect) || die "Cannot prepare: " . $dbh- +>errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); while (@data = $sth->fetchrow_array()) { my $data_to_store = crypt($data[1], $private_key); $data_hash{$data[0]} = $data_to_store; } untie %data_hash; $sth->finish();
And this is re-run every time there's a change to the data_db database. So, essentially, the DBM file gets completely re-written every time there's any change to the data_db database.

It seems to work just fine - the DBM file is checked on occasion and always mirrors the sql database.

BUT - there's always a but - if I ftp a copy of the data_dbm_file from the server onto my (Windows) PC, and open it in Notepad, among the long list of data I find this sort of thing:

bogA4dxaWfeMo:Namen_ bogA4dxaWfeMo:Namen_ bogA4dxaWfeMo:Namen bogA4dxaWfeMo:Name bogA4dxaWfeMo:Namen_0 bogA4dxaWfeMo:Namen_0 bogA4dxaWfeMo:Namen bogA4dxaYbogA4dxaWfeMo:Name bogA4dxaWfeMo:Name bogA4dxaWfeMo:Namen acbogA4dxaWfeMo:Namen abogA4dxaWfeMo:Namen
Multiple entires with slight variations of old name/value pairs that were thought to be deleted long ago. There can be dozens of them for a single name. But they don't appear in a listing of the contents of the DBM file if we do:
tie %h, "DB_File", "data", O_RDWR|O_CREAT, 0666, $DB_HASH or die "Cann +ot open file 'data': $!\n"; print "Content-type:text/html\n\n"; foreach my $key (sort keys %h) { print "<br>$key -> $h{$key}\n"; }
This outputs just the expected name/value pairs.

What is all this extra cruft? Is this due to something about our re-writing sub-routine? Or a typical result in a DBM file? Or something else?


Time flies like an arrow. Fruit flies like a banana.

In reply to Tie data hash to DBM file leaving massive cruft? by punch_card_don

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (3)
    As of 2018-05-27 18:45 GMT
    Find Nodes?
      Voting Booth?