Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that reads from a data file, but if I change the content of the data file , the script keeps outputting the same thing even after I change the contents of the data file. UNLESS, place (any) query string after the path. If i place a querystring after the path, it automatically updates the page every time i update the data file. Does anyone know why it does this and if I can get the page to update without placing a querystring????? Here is the code for the script it is very simple:
#!/usr/bin/perl print "Content-type: text/html\n\n"; open(FILE, "<wr.dat") or die "Cannot open wr.dat for wr"; $rdstr = <FILE>; print "<HTML><HEAD></HEAD><BODY bgcolor=ff9900>\n"; print "$rdstr\n"; print "</BODY></HTML>\n" close(FILE);

Replies are listed 'Best First'.
Re: querystring updating page???
by lhoward (Vicar) on Jun 08, 2000 at 17:48 UTC
    What you're encountering is a caching problem.

    Either your browser, the webserver, or something in-between (like a caching HTTP proxy) is caching the page. When it sees your next request it thinks "oh, I've already gotten this page, so I'll ust the one I got before and stored". What you need to do is put some HTTP headerers on your page so it isn't cached. Setting your HTTP headers as follows shout make pretty sure that it won't be cached.

    Content-type: text/html Expires: Mon, 26 Jul 1997 05:00:00 GMT Cache-Control: no-cache, must-revalidate Pragma: no-cache
RE: querystring updating page???
by sean (Beadle) on Jun 08, 2000 at 17:52 UTC
    Sounds like it might be getting cached by your browser (or a cache somewhere between you and the httpd).. If you're using netscape, try holding down shift when you click 'reload'. If thats the case, one fix could be to add
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    to the HEAD section of your html.

    UPDATE

    As merlyn points out below, this is wrong. Don't do it.
      sean gave the classic WRONG cargo-cult answer:
      If thats the case, one fix could be to add
      <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
      to the HEAD section of your html.
      vroom - please trace down the six people that gave upvotes to this post and shoot them or remove their voting privileges.

      This is wrong. Has been wrong since day one. The spec says that "Pragma" is for client to server, not server to client. That it keeps getting repeated in every online "beginners guide" doesn't make it any more right.

      For a proper explanation of how wrong this is, and what you should do instead, type in "caching tutorial" into Google, which gives something like this caching tutorial as the first or second hit.

      And please stop passing this cargo cult untested crap around. {sigh}

      -- Randal L. Schwartz, Perl hacker

        As someone who voted for the post, I'm posting here so that you can vote this down as much as you like, which I think addresses the shooting request.

        When I vote for a post, I don't always vote for it because I've verified that it is technically correct (in this case, since I haven't dealt with HTML as part of a job for several years, I have not the slightest clue what's correct). Other reasons I vote for a post include 'I agree with what was said' or 'I got something out of the post'. In this case I did get something out of the post. I wrote a note to myself to read up on headers. Because the post I'm replying to includes a link to a tutorial, I'm voting for it (while at the same time, I disagree with part of it, namely that I ought to lose my voting privileges, and that I ought to be literally shot--although I don't have a problem with being shot down in exp by people who agree with meryln).

        If there's a problem with why people vote, then perhaps the voting information could more clearly define what acceptable reasons for voting are. Reasons why people vote was discussed in some depth in Yet Another Post on Experience and no official guidelines resulted. Perhaps there should be further discussion and guidelines as a result?

        Ps: On the thread of Killing Posts: this is just the reason why posts with a low reputation shouldn't be automatically deleted ;).

        That's a little harsh -- if a vote either way is that important to people, they're taking it more seriously than anyone intended it.

        Besides that, the parent post wasn't entirely without merit. The shift-reload test in Netscape is one good way of testing whether it's a browser issue or a script issue. (and actually having someone point out the error in the Pragma approach is nice, because it is so widespread)

RE: querystring updating page???
by le (Friar) on Jun 08, 2000 at 17:49 UTC
    I suppose with "query string after the path" you mean the path of your browser's Location field.
    Maybe this is not a perl problem, but you have your page still in your browser's cache... Just a guess.