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