Life, Love, Various Vodka's, Trials and Tribulations of Life in Ukraine

tags: all animals food funny gas_crisis hamsters holidays money photos smoking stupidity tymoshenko ukraine website   

 

Embedding Perl in HTML: or using Perl like JSP or PHP == PSP? // Thu Mar 26 19:25:09 2009

I was starting to wonder whether I should try some PHP, due to the way it allows you to work with a web designer more easily. I have no great desire to construct wonderful HTML and CSS, much better to let someone else do that with something like Dreamweaver.

After learning just enough PHP to realise I didn't like it too much I thought about ways of using Perl in the same way. However... after looking at things like HTML::Mason, Template Toolkit, HTML::Template, HTML::EP and even Text::Templet. Out of all of them HTML::EP came closest, but still not as simple as what I wanted. I just wanted to code in the same way as PHP but with Perl code embedded in the HTML.

So... PSP is born, Perl Server Page! ok... i just think PSP sounds cool and is similar enough to PHP, JSP and ASP :-)

It's simple enough. psp.cgi just takes the html/psp file and translates it into a pure Perl script by turning HTML code into Perl print() statements and leaving the embedded Perl alone.

HTML/PSP source file:

<h1>Heading</h1>
<ul>
<%
foreach $i (0..10) 
{ 
// a simple loop
%>
<li>$i = <%= $i %> </li>
<% } %>
</ul>

Generated Perl code (with added newlines for clarity):
print qq|<h1>Heading</h1>
<ul>|;
foreach $i (0..10) 
{
print qq|<li>\$i = |; 
print qq|$i|;
print qq|</li>|;
}
print qq|</ul>|;

As you can see it also allows '//' for comment lines since otherwise it will look sily using hashes for the perl comments while '//'s for the javascript, but that is optional. The code is eval()'d in a 'Safe' compartment. So far so good.

I've also made a new function called include() available to the eval()'d perl since otherwise you couldn't include another file containing embedded perl in the same way. I may well decided that this is all a very silly idea, but there again maybe not...


#permalink // add a comment // tagged with: