One of the benefits of writing the hangman application as a plug-in is that you no longer need to write a CGI script at all. You can load and use the plug-in from any template, which you can process via a generic CGI script, a mod_perl handler, or perhaps the Apache::Template module.

Here's an example of a self-contained template using the hangman plug-in. All we need to do is to hardcode some variable values at the start of the template:

[%  title = 'Template Toolkit Hangman #5'
    url   = '/tt2/hangman.html'
    words = '/usr/games/hangman-words'
    icons = '/icons/hangman';

    WRAPPER html/page
        html.head.title  = title
        html.body.onload = 'if (document.gf) document.gf.guess.focus( )';

        TRY;
            USE hangman = Games.Hangman(
                words = words
                icons = icons
                url   = url
            );
            hangman.play;
        CATCH;
            CLEAR;
            PROCESS error;
        END;
    END
%]

If you're using Apache::Template to run the application, you can define these variables in the Apache httpd.conf file:

PerlModule          Apache::Template

TT2IncludePath      /usr/local/tt2/hangman/hangman3/templates
TT2IncludePath      /usr/local/tt2/templates
TT2Variable         title  "Template Toolkit Hangman #5"
TT2Variable         words  /usr/games/hangman-words
TT2Variable         icons  /icons/hangman
TT2Params           uri

<Location /tt2/hangman.html>
    SetHandler      perl-script
    PerlHandler     Apache::Template
</Location>

Our three variables, title, words, and icons, are defined using the TT2Variable directive. In addition, we use TT2Params to instruct Apache::Template to make the request URI available as the uri template variable. We previously used url to denote the URL of the hangman application, so we need to make one small change to the template. Using this dynamic uri variable should mean that the value will remain correct even if the application is moved to a new URL. The template should now look like this:

[% 
    # ...etc...

    USE hangman = Games.Hangman(
        words   = words
        icons   = icons
        url     = uri     # now use 'uri' not 'url'
    );

    # ...etc...
%]

The game in Figure D-3 is for you to complete.

Figure D-3

Figure D-3. White to play and mate in three moves