What exactly do you mean by "touching the hash" and "dirty the hash"? $db->sync will not actually write anything unless the hash contents have actually been modified in some way (i.e., there must be some cached buffers in order to "Flush any cached buffers to disk"). You cannot force a write with $db->sync. For example:

$db->sync; # not modified $hash{foo} = 'bar'; # not modified $db->sync; # MODIFIED @hash{qw<one two three>} = 1..3; # not modified $db->sync; # MODIFIED delete $hash{two}; # not modified $db->sync; # MODIFIED $db->sync; # not modified

Edit (Further thoughts): If you have definitely modified the hash and after a $db->sync the file still appears to be unmodified, you should be checking the return of $db->sync, which unfortunately returns 0 on success; if it returns -1, it failed, and a more detailed error is available in $!. 1 is also an error: "1 generally (but not always) means that the key specified did not exist in the database". Finally, remember by default stat and friends operate with whole-second resolution, so if you were to run my above code and use the file's mtime as your test, probably all lines would appear not modified. See stat (especially the caveats!), and consider using other metrics such as file size and Digest::MD5 checksum, but beware the latter may negatively impact performance if the file is large or checked very frequently.

use strict; use warnings; omitted for brevity.

In reply to Re: When does sync not touch the db file? by rjt
in thread When does sync not touch the db file? by steffi2

Title:
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, details, 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, summary, 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.