Apologies in advance, I am a Perl and programming novice. This is probably a Stupid Question, but I've looked for an answer in the archives and I still can't figure it out.

I have a CGI script that (1) writes to a datafile, then (2) immediately reads that file in order to print to a webpage. The problem is that the changes I make (deletions, actually) do not "take" immediately. For example, if I delete record #150 (by using splice--backwards, if there's more than one record being deleted), the page refreshes and record #150 is still on the page (even though it's gone from the datafile). If I reload the page (not, in case you're wondering, by resubmitting the form data, which would try to re-delete the record), then and only then will record #150 be gone from the webpage.

Now here's the weird part (to me): this happens only with the last three records. Suppose the last three records are 'A', 'B', and 'C'. For each of the last three records, a different thing happens if I try to delete it:

If I delete record $array[-1], then on the webpage, nothing happens until I refresh (without resubmitting). Then and only then the record (in this case, 'C') disappears.

If I delete record $array[-2], or 'B', then on the webpage, the last three records read: 'A', 'C', 'C'. THEN if I refresh (without resubmitting), the last two records are (as originally expected) 'A' and 'C'.

Finally, if I delete record $array[-3], or 'A', then on the webpage, the last three records read: 'B', 'C', 'C'. And again, if I refresh, the last two records are instead, as originally expected, 'B' and 'C'.

Whenever I delete any of the records in the beginning or middle of the file (before the $array[-3] position), the item(s) immediately disappear(s).

My only thought after research is that it might have something to do with a needed file lock, but I don't know anything about that, and it sounds implausible anyway. (The datafile is written-to and closed before it is reopened for reading.)

Please expose what embarrassing error I have committed! Or, if you need more info, let me know.

--ghopper :-)


In reply to Datafile doesn't update before being read by ghopper

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.