Listing 2. PigLatin.pm.
#!/usr/bin/perl -w
package Apache::PigLatin;
use strict;
use diagnostics;
# Import constants for mod_perl
use Apache::Constants qw(OK DECLINED NOT_FOUND);
sub handler
{
# Get the Apache request object
my $r = shift;
# Only handle text/html files
return DECLINED unless ($r->content_type eq
"text/html");
if (open(FILE, $r->filename))
{
# Send an appropriate MIME header
$r->send_http_header;
# Slurp up files at once
undef $/;
# Grab the file's contents
my ($contents) = (<FILE>);
# Turn headlines into Pig Latin
$contents =~ s|(<h\d>)(.*?)(</h\d>)|$1 .
pl_sent($2) . $3|eigs;
# Print the contents
$r->print($contents);
# Close the file handle
close FILE;
# Indicate that all went well
return OK;
}
# produce an appropriate error message
else
{
return NOT_FOUND;
}
}
sub piglatin_word
{
my $word = shift;
return substr($word, 1) . substr($word, 0, 1) .
"ay";
}
sub pl_sent
{
my $sentence = shift;
return join (' ',<\n>
map {piglatin_word $_} split (/\s+/, $sentence));
}
1;