F.3. Storing Persistent Data
While hidden fields are useful when working with forms, it's often necessary to store persistent data in a more general way. Embperl utilizes Apache::Session to do this job. Apache::Session is capable of storing persistent data in memory, in a text file, or in a database. More storage methods may be supported in the future. Although you can simply call Apache::Session from an Embperl page, Embperl can call it for you. All you need to do is to put user data in the hash %udat. The next time the same user requests any Embperl page, %udat will contain the same data. You can use this approach to keep state information for the user, and depending on your expire settings, you can also keep state between multiple sessions. A second hash, %mdat, can be used to keep state for one page for multiple users. A simple example would be a page hit counter:
The page is requested [+ $mdat{counter}++ +] times
since [+ $mdat{date} ||= localtime +]
This example counts the page hits and shows the date when the page is first requested. (See the hangman game at the end of this appendix for more examples of %udat and %mdat.) You don't need to worry about performance—as long as you don't touch %udat or %mdat, no action is taken.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access