|
My PSP (Perl Server Page) Module so far...
// Thu Apr 2 07:01:44 2009
Zestoi::Plugin::PSP is designed to be (designed?) the most simple ever module implementing Perl Server Pages aka Perl embedded in HTML (or any text file)
print Zestoi::Plugin::PSP::parse("file.psp");
|
To embed perl code:
- e.g:
<% for $i (0..10) { %>
- may be be multiline (of course)
To embed the value of a perl expression:
- e.g:
<h1> <%= $heading %> </h1>
- the expression is just
print()ed like print $heading;
- whitespace is optional
To include another pl/psp/html/etc file:
include "file.psp";
include "file.pl";
include "file.html";
|
- must be within a
<% %> block
- when used normally,
include is a function run at eval time. when saving the generated perl code (e.g: for debug) the contents of the file is lexically embeded recursively unless it encounters a .pl in which case it is replaced by require.
To add comments:
# a comment
// another comment
|
- so you don't have to use
#'s as well as javascript //'s in the same file
Global variables:
$cgi: CGI object
%args: CGI->Vars
and that's it! so... this nasty bit of code is actually valid:
<%
sub sql_query
{
qw(1 foo 2 bar 5 spoon);
}
%>
<html>
<head>
<% sub foo { gmtime }; %>
<script language="Javascript">
function district(name)
{
<%
# this is a perl comment
my @q = sql_query("SELECT id,name FROM districts");
my %id2name = ();
// this is a perl comment js style
while (scalar @q)
{
my ($id,$name) = (shift @q, shift @q);
%> hide("district_<%= $id %>"); <%
$id2name{$id} = $name;
}
%>
// this is a normal js comment
show(name);
}
</script>
<% $today = foo(); %>
</head>
<body>
district id's are <%= join(",", keys %id2name) %><br/>
and the date is <%= $today %>
</body>
</html>
|
...and ends up as the following perl code after (pre)processing... (psp -e foo.psp > foo.pl)
sub sql_query
{
qw(1 foo 2 bar 5 spoon);
}
print qq|
<html>
<head>|; sub foo { gmtime }; print qq|
<script language="Javascript">
function district(name)
{|;
# this is a perl comment
my @q = sql_query("SELECT id,name FROM districts");
my %id2name = ();
## this is a perl comment js style
while (scalar @q)
{
my ($id,$name) = (shift @q, shift @q);
print qq| hide("district_|;print $id ;print qq|");|;
$id2name{$id} = $name;
}
print qq|
// this is a normal js comment
show(name);
}
</script>|; $today = foo(); print qq|
</head>
<body>
district id's are|;print join(",", keys %id2name) ;print qq|<br/>
and the date is|;print $today ;print qq|
</body>
</html>|;
|
...which in turn when run creates the following html... (perl foo.pl > foo.html)
<html>
<head>
<script language="Javascript">
function district(name)
{ hide("district_1"); hide("district_2"); hide("district_5");
// this is a normal js comment
show(name);
}
</script>
</head>
<body>
district id's are1,2,5<br/>
and the date isWed Apr 1 19:51:23 2009
</body>
</html>
|
some of the white space is missing due to the preprocessor trying to make the output a bit prettier.
output is correct when going straight to html from psp though... (psp foo.psp > foo.html)
<html>
<head>
<script language="Javascript">
function district(name)
{
hide("district_1"); hide("district_2"); hide("district_5");
// this is a normal js comment
show(name);
}
</script>
</head>
<body>
district id's are 1,2,5<br/>
and the date is Wed Apr 1 20:07:51 2009
</body>
</html>
|
district id's are 1,2,5
and the date is Wed Apr 1 20:25:33 2009
#permalink //
add a comment //
tagged with: programming websites perl php
|