Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: saving a random number while generating a new one

by Sinistral (Prior)
on Mar 30, 2011 at 14:38 UTC ( #896430=note: print w/replies, xml ) Need Help??


in reply to saving a random number while generating a new one

What you're encountering is a fundamental misunderstanding of how CGI scripts work. When your user is interacting with your HTML page and CGI script, here's what's happening:

  1. User's browser requests index.html, receives the HTML, and renders it on their local computer
  2. User clicks the roll the dice link, which sends another request for dice.cgi
  3. The web server receives the request for dice.cgi, knows that it is a script, and runs Perl to produce the output
  4. The web server collects the output from your script (the print in your Perl code) and sends it out the network connection to the user's browser
  5. The user's browser receives the HTML output by your Perl code and renders that

Note that your script is running again and again as if it had never run before. It has no state, and the code you've shown has no way of retaining state. As you mention, you can use a file to save previous runs. As an alternative, what you might do is to use CGI::Session to have a session for the user that retains all the runs that they've made. If you don't want something that heavy, then you could use DBI along with DBD::SQLite to have a SQLite database (which is a flat file, ultimately) for your data storage. If you don't want DBD::SQLite, you could use a full blown database engine such as DBD::mysql or DBD::Pg.

Based on your code, it seems that you are not yet familiar with HTML/CGI idioms or syntax, so I would actually pursue a simple text file (Perl excels at reading and parsing simple text files) as your data collection mechanism

  • Comment on Re: saving a random number while generating a new one

Replies are listed 'Best First'.
Re^2: saving a random number while generating a new one
by raybies (Chaplain) on Mar 30, 2011 at 15:13 UTC
    Good responses, and since it hasn't been mentioned and (though it's a pain) you can also pass state data along using "hidden" parameters (if you've got a form/submit), and/or using cookies. Of course there are security issues with some of these approaches.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://896430]
help
Chatterbox?
[choroba]: Russia didn't send them officially, they're on vacation in Korea
[LanX]: no that was Syria! ;-)
[LanX]: And the reason is: The world’s best players aren’t at this year’s Games after the NHL stopped its athletes from leaving in the middle of the season. ... LOL
[LanX]: horoba: you like ice hockey?
[choroba]: Like is a too strong word. But you can't evade it here.
LanX (for people wondering: yes field hockey still exists and is even more popular than Hasslhoff in Germany)
[Corion]: LanX: Ah, that explains a lot :)
[marto]: Nobody we spoke to in Berlin had a clue who Hasslhoff was
[erix]: heh: The Dutch do one thing very, very well
[LanX]: the sidekick of this blonde playboy model which was saving lives with her red bathing suit ... (what was her name again ...)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2018-02-23 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (303 votes). Check out past polls.

    Notices?