Chapter 11. Recipes
No, we are not going teach you how to make a delicious tofu and soybean stew. But this is almost as good. This chapter shows how to do some common Mason tasks, some of them with more than one implementation.
Sessions
For many of our
session examples, we will be using the
Apache::Session
module. Despite its name, this module doesn’t
actually require mod_perl or Apache, though that
is the context in which it was born and in which
it’s most often used. It implements a simple tied
hash interface to a persistent object.[22] It has one major
gotcha: you must make sure that the session object gets cleaned up
properly (usually by letting it go out of scope), so that it will be
written to disk after each access.
Without Touching httpd.conf
Here is an example that doesn’t involve changing any of your Apache configuration settings. The following code should be placed in a top-level autohandler. Any component that needs to use the session will have to inherit from this component, either directly or via a longer inheritance chain.
It uses cookies to store the session.
<%once>
use Apache::Cookie;
use Apache::Session::File;
</%once>
<%init>
my %c = Apache::Cookie->fetch;
my $session_id =
exists $c{masonbook_session} ? $c{masonbook_session}->value : undef;First, it loads the necessary modules. Normally we recommend that you do this at server startup via a PerlModule directive in your httpd.conf file or in your handler.pl file to save memory, but we load them here just to show ...
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