Monday, May 24, 2004

simplepage.pl

Here is the basic version of a simple templating and non-messy Web page
script.

HTML is bloody messy.... allways messy- It's my pet peeve I guess..




#!/usr/bin/perl

use
warnings;
use strict;
# this is a proof of concept test designed to
# find out whether or not I can use POE and HTTP::Response
# to make a fully dynamic webpage with minimal messyness and
# uglyness.

# the modeling will be at three levels:
# Structure
# Data
# Control



use POE qw(Component::Server::TCP Filter::HTTPD);
use HTTP::Response;

use constant BindAddress =>
'127.0.0.1';
use constant BindPort =>
'8087';
use constant Filter =>
'POE::Filter::HTTPD';


sub server{

my
$alias = shift;
my
$port = shift;

POE::Component::Server::TCP->new
(Port =>
$port,
Alias =>
$alias,
ClientInput => \&handle_client_input,
# Error => \&error_handler,
ClientFilter => Filter,

# InlineStates => {
# req => \&make_request,
# }
);
}

sub handle_client_input{
# get link data from remote connection.
# call functions to parse link and change control structures.
# cycle output.

my ($kernel, $heap, $request) =
@_[ KERNEL, HEAP, ARG0 ];

# Filter::HTTPD sometimes generates HTTP::Response objects.
# They indicate (and contain the response for) errors that occur
# while parsing the client's HTTP request. It's easiest to send
# the responses as they are and finish up.
# (This if statement - including comments are cribbed from poe.perl.org)

if ( $request->isa("HTTP::Response") ) {
$heap->{client}->put($request);
$kernel->yield("shutdown");
return;
}


my
$response = HTTP::Response->new(200);
my
$session = control_data($request->{_uri});
my
$stuff = make_template();
$stuff = generate_content($stuff,$session);
$response->push_header( 'Conten-type', 'test/html' );
$response->content($stuff);

$heap->{client}->put($response);
$kernel->yield("shutdown");

#control_data() changes the $control data structure to reflect the
#choices made by the client. this data structure will indicate what
#data will be presented later on by the server to the client.

#make_template() gives $stuff all the structure junk... $stuff
# is just a string full of tags and things for later routines to
#fill in with "stuff"

#generate_content() will stick the "stuff" in stuff based on the
#control structure ($session). special tags put in the $stuff $string
#will be substituted with "stuff" fron this routine.
#e.g $stuff=~ s/\[\% title \%\]/$session->{title}/;
# or
# $stuff =~ s/\[\%(w*?)\%\]/$session->{$1}/;
#which takes away a tag like "[%Title%]" and
#replaces it with the contents of $session->{Title}.


#just read a bit about template toolkit *laugh* go figure..


}

sub control_data{
# control_data() changes the $control data structure to reflect the
# choices made by the client. this data structure will indicate what
# data will be presented later on by the server to the client.

# control data recieves $request->{_uri} (see documentation for
# HTTP::Response) and uses it to create (in future modify)
# the %control datastructure which it then returns.
# the hash %control will contain things like the page title,
# lists of links, and files/locations from which to grab data.
my $input=shift;
my
$control;

$control->{Title}='Test Title';
$control->{Index}='Test Index<b><a href=';
$control->{Index}.='"nothing.html">Nothing!</a>';
$control->{Body}='The Body!!<p></p>The Body!!!!!';
$control->{Footer}="$input";
return
$control;
}

sub make_template{
#make_template() returns all the structure junk... $crap
#is just a string full of tags and things for later routines to
#fill in with "stuff"

my $crap;

$crap = '<html><body>';
$crap .='<table><tr>'.
'<td>'.
'%%Index%%'.
'</td>'.
'<td>'.
'%%Body%%'.
'</td>'.
'<td>'.
'%%Index%%'.
'</td>'.
'</tr></table>'.
'That\'s that<br></br>'.
'%%Footer%%'.
'</body></html>';
return
$crap;
}


sub generate_content{

my
$stuff = shift;
my
$session = shift;

#generate_content() will stick the "stuff" in $stuff based on the
#control structure ($session). special tags put in the $stuff string
#will be substituted with "stuff" from this routine.
#e.g $stuff=~ s/\[\% title \%\]/$session->{title}/;
# or
# $stuff =~ s/\[\%(w*?)\%\]/$session->{$1}/g;
#which takes away a tag like "[%Title%]" and
#replaces it with the contents of $session->{Title}.


#just read a bit about template toolkit *laugh* go figure..

#currently this routine does not include data from outside
#files.. this must change quickly.
$stuff =~ s/%%(\w+?)%%/$session->{$1}/g;
print "and stuff is $stuff\n";
return
$stuff;
}
server(
'debug',BindPort);
$poe_kernel->run();


0 Comments:

Post a Comment

<< Home