Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Datafile doesn't update before being read

by George_Sherston (Vicar)
on Jun 06, 2002 at 19:36 UTC ( [id://172328]=note: print w/replies, xml ) Need Help??


in reply to Datafile doesn't update before being read

Good that you post some code. I don't entirely agree with respected sibling Sifmole - I'd encourage you not to be parsimonious with your code posting. As it was, your question was about a thing that happens when you refresh your web page, if I understood it, but the code you posted doesn't seem to cover that.

So my first suggestion is, don't be shy, post away (on the other hand other monks may be working with a more current version of SOPW::Psychic than I have and may be able to answer based on what we have).

My second suggestion is - you say

I have a CGI script that (1) writes to a datafile, then (2) immediately reads that file in order to print to a webpage.

- now there's probably a good reason for this, but on the face of it, why bother with the read at (2)? - you've already got the data in memory, so couldn't you just send it to the browser?

§ George Sherston
  • Comment on Re: Datafile doesn't update before being read

Replies are listed 'Best First'.
Re: Re: Datafile doesn't update before being read
by ghopper (Novice) on Jun 06, 2002 at 20:27 UTC
    I guess I was trying to keep my program "modular," reduce the number of global variables to a minimum, so I guess that's why didn't just use the array I made earlier later.

    In retrospect, however, you're 100% right. I simply made @strmeta a global variable and didn't bother reprocessing the data. Then the problem went away and as an extra bonus, the page loads faster.

    Thanks a lot, George!

    --ghopper

      Your'e welcome. I (and others doubtless) applaud your desire to be modular. How about this, then?: take your subroutine for printing output from an array. Not sure what you're calling this - say print_page. Now, instead of making @strmeta a global, make a local variable and pass a reference to it to the subroutine.

      That is, call your subroutine like
      my @strmeta = ..... [...] print_page(\@strmeta);
      Then in the subroutine make the first line
      my $strmeta = shift;
      And thereafter, where before you had @strmeta use @$strmeta, and where you had $strmeta[$foo] put $strmeta->[$foo] - which will "de-reference" (i.e. get the data out of) your reference $strmeta.

      Then, if you set it up like that, you can call the same subroutine from two different points in your script - (a) when you've created @strmeta from your form input, and (b) when you've created it from retrieved data. Your subroutine won't care where the data comes from, so long as it comes in an array reference.

      For your last trick, put print_page() in a module, and use it in all your other scripts!

      Enjoy!

      § George Sherston

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://172328]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-25 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found