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:
- User's browser requests index.html, receives the HTML, and renders it on their local computer
- User clicks the roll the dice link, which sends another request for dice.cgi
- The web server receives the request for dice.cgi, knows that it is a script, and runs Perl to produce the output
- 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
- 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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.