Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I have only recently started learning to use mod_perl, but, so far, I like what I see. There is one thing I couldn't find, though. I wanted a simple application controller, which would handle some of the grunt work for me. I wanted to separate the logic and display into modules and templates. This didn't seem like a lot to ask, but I couldn't find what I was looking for on CPAN.

CGI::Application came close, and merlyn's newly released CGI::Prototype looks interesting (apparently this is a recently popular subject), but both are geared towards CGI. They can doubtlessly be made to work with mod_perl via Apache::Registry, but I would rather run my controller directly as a PerlHandler. I got [a bit too] excited when I saw Apache::MVC, but, alas, that is specifically for Maypole. Maypole looks very nice indeed, but it's not quite what I was looking for. I also tried searching CPAN for 'apache controller', and other similar permutations. I found many things that seemed specific to one framework or another, but nothing generic that had the attributes I wanted. Perhaps this was a failing -- on my part -- to search for the relevant terms.

So I thought, "what the heck," and rolled my own. I made it about as simple as I could possibly imagine, while still being useful, and began to use it for my personal website. It seems to work fairly well, but I was interested in some feedback. The main things I was wonder are:

  • Is this something I should put on CPAN? There are already a number modules that seem to solve the same problem, but I couldn't find one that solved it the way I would like. So far my module seems to do that, so perhaps it would be useful to someone who thinks like I do?
  • Can anyone think of a better name than Apache::Controller? I don't think it's terrible, but it strikes me as somewhat pretentious. I am not attempting to write the controller for mod_perl, and invading the Apache namespace makes me feel a little uneasy. Any suggestions?
  • How is the documentation? Sometimes I have a tendency to be wordy, and it's something I intentionally try to avoid. Have I succeeded with the documentation? Perhaps there are simpler ways to present the information.
  • How is the interface? It is extremely simple. Perhaps too simple? Any suggestions on what I could do differently would be helpful.

Here is the documentation I have written:


Apache::Controller is a generic controller for mod_perl web applications. It will map each incoming request to a certain action module, fill out a template, and send the resulting data to the client.

    package MyApplication::Controller;
    use lib "/my/application/root/modules";
    use Apache::Controller;
    my $controller = Apache::Controller->new(
        ROOT => "/my/application/root",
        action_modules => {
            _default => 'MyApplication::Default',
            manage   => 'MyApplication::Manage',
            view     => 'MyApplication::View',

    sub handler { $controller->handler(shift) }


Creates a new Apache::Controller object. Required named parameters are ROOT and action_modules.

ROOT defines the root directory where this application resides.

action_modules is a hash that maps action names to modules. The keys are names of the actions, and the values are corresponding modules. The modules should each contain a subroutine called handler.

Handles the request. Requires one argument, an Apache::Request object.

The first stage of request handling is URI parsing. The URI is split into an action name and arguments portion. The action name looks like a top-level directory in the URI, and the arguments consist of anything following the action name. For example, a request to would call the view action with arguments page1/print.

If there is no action name found in the URI, _default is used. This is analogous to index.html. Handling of arguments is currently left entirely to the action module, but may be standardized in the future.

Once the action name is parsed, the action_modules hash is consulted to find an action module. If one is not found, NOT_IMPLEMENTED is returned. If one is found, it is checked for a handler subroutine. If the handler is not found, NOT_IMPLEMENTED is returned.

The handler subroutine can return one of two things: a redirect URL, or a list of parameters. This determination is done by counting the number of elements returned. If there's only a single element returned, it is assumed to be a redirect URL. If there is an even number of elements returned, it is assumed to be a list of parameters. If there is an odd number of elements, or no elements at all, a SERVER_ERROR is returned.

The final stage in request handling is output. First, a template is looked for in the ROOT/templates directory. The template is assumed to have the same name as the action, with the .tmpl file extension. For example, an action called view would have a template file named ROOT/templates/view.tmpl.

If found, the template is loaded and passed the parameter list, then output to the client. If no template is found, a default page is populated, which lists each element in the parameter list. This is a debug feature which may be disabled in the future.

I have decided not to include the code in this post, because I'd rather not (yet) concentrate the Power of the Monks in that direction. Right now I'm more concerned with getting the documentation, name, and interface right, than with code cleanliness and correctness. I will nail down those bits if there's any further interest in this thing.

In reply to RFC: Apache::Controller by revdiablo

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (1)
    As of 2021-05-10 03:00 GMT
    Find Nodes?
      Voting Booth?
      Perl 7 will be out ...

      Results (104 votes). Check out past polls.