NAME

Apache::ContentHandler - mod_perl extension for uniform application generation.

SYNOPSIS

    use Apache::ContentHandler;

    @ISA = 'Apache::ContentHandler';

    sub handler {
      my $r = shift;
      my $algometer = new Apache::Algometer($r);
      my $result = $algometer->run;
      return $result;
    }

    sub _init {
      my $self = shift || die 'need $self';
      $self->SUPER::_init(@_);

      # overrides
      $self->{title}     = 'Project Algometer';
      $self->{subtitle}  = "Version $VERSION";
      $self->{default_action} = 'hello';
      # other variable definitions
    }

    sub hello {
      return '<P>Hello World</P>';
    }

DESCRIPTION

Apache::ContentHandler is a generic framework for creating mod_perl based applications. It provides a basic event mechanism and a subclassable framework for customizing actions.

The synopsis shows a very simple example of what it can do. In this case, we set the default_action to 'hello', which is automatically executed. Hello in this case outputs a simple paragraph. Nothing big, but it is very simple. Note that this app runs as-is in both CGI and mod_perl.

Rapid Prototyping

This does not demonstrate the real power of ContentHandler. The real power comes from rapid prototyping. For example, if we modifed the example above to read:

    sub hello {
      my $self = shift || die 'need $self';
      my $s = '';
      $s .= '\<A HREF="$self-\>{url}?action=make"\>Make\</A\> something.';
      return $s;
    }

Then the page will output a url for the application that includes "action=make" as a url parameter. This will tell ContentHandler to run the method make when executed. But, 'make' does not exist at this time. That is ok, because ContentHandler will deal with it by putting a standard page up explaining that that feature is not yet implemented. This allows you to quickly prototype one page, and move on to the rest of the functionality one piece at a time.

I have used this style with clients on several different projects and they were all extremely happy to get something tangible in a very short period of time, usually 5 minutes to get the first page up and running with skeletal functionality. From there, it is a very interactive process with the client driving on one machine and commenting, and me coding away at another machine as they talk.

PUBLIC METHODS

  • $ch = Apache::ContentHandler->new

      Creates a new ContentHandler. You should not override this, override
      _init instead.
  • $hc->run

    The main application structure. Provides for a standard header, body, and footer. You probably do not want to override this, override the individual methods instead.

PROTECTED METHODS

  • _init

    Private: called by new. Override to put your application specific variables here.

  • $val = $self->arg($key)

    Returns a CGI/mod_perl parameter for the key $key.

  • @keys = $self->args

    Returns a list of all of the mod_perl/cgi parameters.

  • $s = $hc->header

    Returns a string containing the preheader, an HTML title, and a postheader. You probably do not want to override this unless you want a different type of title.

  • $s = $hc->work

    Runs a method corresponding to the $action parameter, or the default action, and returns the content as the body of the document. If the $action does not exist, then it puts up a page stating that. This makes rapid prototyping very easy and quick.

  • $s = $hc->footer

    Returns a string containing the prefooter, and postfooter. This used to have a standard footer as well, but I found it annoying.

  • $s = $hc->errors

    Returns a dictionary list detailing the contents of the error hash, if any.

  • $s = $hc->preheader

    Returns the contents of the preheader. Override to add something before the title.

  • $s = $hc->postheader

    Returns the contents of the postheader. Override to add something after the title.

  • $s = $hc->prework

    Returns the contents of the prework. Override to add something before the body.

  • $s = $hc->postwork

    Returns the contents of the postwork. Override to add something after the body.

  • $s = $hc->prefooter

    Returns the contents of the prefooter. Override to add something before the footer.

  • $s = $hc->postfooter

    Returns the contents of the postfooter. Override to add something after the footer.

  • $s = $hc->reportError

    Sends an email to the addresses listed in error_email, detailing an error with as much debugging content as possible. Used for fatal conditions.

  • $s = $hc->dbi

    Returns a DBI connection. Override _init and add values for dbi_driver, dbi_user, and dbi_password to make this connection.

  • $s = $hc->sqlToTable

    Returns an HTML representation of a SQL statement in table form.

  • $s = $hc->sqlToArrays

    Returns an array representing a SQL query.

  • $s = $hc->sqlToHashes

    Returns a hash representing a SQL query.

  • $s = $hc->query1

    Returns a single value from a SQL query. The query must return a single column and row (ie SELECT name FROM users WHERE id=42).

LICENSE

(The MIT License)

Copyright (c) 2001 Ryan Davis, Zen Spider Software

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

AUTHOR

Ryan Davis <ryand-ch@zenspider.com> Zen Spider Software <http://www.zenspider.com/ZSS/>