|
How-to - ModPerl custom Perl Module
This document provides a rudimentary Perl Module which can be used as a starting point for creating more sophisticated modules later.
§ Source code
This module is one that is tailored specifically to the web site. It is responsible for generating consistent headers and footers (which your Perl scripts will call upon).
Create the following file, which was referenced in your virtual host configuration file by the "PerlRequire" directive:
- /internet/com/example/lib/Example.pm
Then, add the following content to that newly created Perl Module file:
package www_Example;
use strict;
use warnings;
# -----------------------------------------------------------------------------
# Additional required modules.
# -----------------------------------------------------------------------------
use Apache2::Reload; # --- Used on development servers (not production servers)
return 1;
# =============================================================================
# Constructor.
# =============================================================================
sub new {
# -----------------------------------------------------------------------------
# Define internal hash.
# -----------------------------------------------------------------------------
my $self = {}; # --- Allocate new memory
bless($self, shift); # --- Create object
# -----------------------------------------------------------------------------
# Initialize internal variables.
# -----------------------------------------------------------------------------
my $r = $self->{r} = shift; # --- This is the Apache2::RequestRec object
$self->{ROOT} = $r->dir_config('ROOT'); # --- Obtain the root directory path
# -----------------------------------------------------------------------------
# Return the blessed object.
# -----------------------------------------------------------------------------
return $self;
} # -x- sub new -x-
# =============================================================================
# Sends the header section of the HTML code, containing the initial portion of
# the web site, to the client.
#
# The Content-type is also set to "text/html" by this subroutine.
#
# Parameters:
# $self hashref
# $args hashref
# =============================================================================
sub send_header {
# -----------------------------------------------------------------------------
# Initialize internal variables.
# -----------------------------------------------------------------------------
my $self = shift;
my $args = shift;
my $title;
# -----------------------------------------------------------------------------
# Generate dynamic title. Meta tags (for keywords, stylesheets, etc.) can also
# be created at this point.
# -----------------------------------------------------------------------------
if ($args->{title} eq '') {
$title = 'Example web site';
} else {
$title = '[Example.com] ' . $args->{title};
}
# -----------------------------------------------------------------------------
# Send the header.
# -----------------------------------------------------------------------------
$self->{r}->content_type('text/html'); # --- Specify MIME type
$self->{r}->print('<html>
<head>
<title>' . $title . '</title>
</head>
<body bgcolor=white>
<!-- HTML header goes here -->
');
# -----------------------------------------------------------------------------
# We're finished.
# -----------------------------------------------------------------------------
return;
} # -x- sub send_header -x-
# =============================================================================
# Sends the footer section of the HTML code, containing the final portion of
# the web site, to the client.
#
# Parameters:
# $self hashref
# $args hashref
# =============================================================================
sub send_footer {
# -----------------------------------------------------------------------------
# Initialize internal variables.
# -----------------------------------------------------------------------------
my $self = shift;
my $args = shift;
# -----------------------------------------------------------------------------
# Send the footer.
# -----------------------------------------------------------------------------
$self->{r}->print('
<!-- HTML footer goes here -->
</body>
</html>
');
# -----------------------------------------------------------------------------
# We're finished.
# -----------------------------------------------------------------------------
return;
} # -x- sub send_footer -x- |
The header and footer HTML code can contain tables and/or stylesheet code to present beautiful graphics/layout to the user. The footer could also contain copyright information and additional navigational aids.
Previous | Index | Next
|
|