tags:

views:

129

answers:

4

Hi all:

I've researched online and found several interesting Perl modules/frameworks, such as HTML:Mason, HTML::Embperl, or the MVC Catalyst framework, etc,which can let me embed Perl inside html, similarly like PHP code inside html.

However, my Perl project must be uploaded to uni server where only limited privilege and resources are provided.

For instance, Apache version 1.3.3 & Perl version 5.8.0 (lower than Catalyst's requirements)

I've used a script to check all installed Perl modules, only those names contain the word "html":

HTML::HeadParser    2.17
HTML::Entities  1.23
HTML::Filter    2.09
HTML::LinkExtor 1.31
HTML::Parser    3.26
HTML::PullParser    2.06
HTML::TokeParser    2.24
HTML::Tagset    3.03
HTML::Form  0.03

I am afraid none of them can let me embed Perl directly into html.

I know I can use simple print statement together with "heredoc" to print everything on html page inside Perl/CGI, but I reckon that violates the MVC design paradigm and is less flexible and more complicated to develop, mainly because now the business logic is messed up with html markups.

My current solution is to use jQuery to trigger AJAX requests to load html into specific tags from client-side. So in this case, Perl is only used to provide server-side data access, manipulated the related data and provide JSON formatted responses to the AJAX requests.

I wonder is there a better way to do that? I can hardly change the server status and I don't think the system admin would be that generous to install any other Perl modules.

Updated Info:

The main reason for embedding Perl into html is that I am very new to CGI programming, and since I am more familiar with PHP and jQuery, I'd like to know if there is a proper way to embed Perl directly into html, so I can finish off the client part very quickly and concentrate on the server-side.

Say, something like this:

<div id='user_status'>Your last visit was :[% getLastVisitDateTime($userId)%]</div>

Please bear with my little knowledge of Perl/CGI and many thanks to the help in advance.

Updated 2nd: Followed the Template Toolkit website instruction, I installed this module on my own MacBook Pro but unfortunately I cannot install it onto uni's server due to permission reason:

Warning: You do not have permissions to install into
 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi 
at /usr/lib/perl5/5.8.0/ExtUtils/Install.pm line 84.
mkdir /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-
multi/auto/Template: Permission denied at /usr/lib/perl5/
5.8.0/ExtUtils/Install.pm line 137
make: *** [pure_site_install] Error 255

So unfortunately I am now seeking for other ways...

Okay, it seems HTML::Mason cannot be installed for the very same reason. Therefore I am afraid I must find a .pm only solution so that I don't have to install anything to the uni server's perl environment...

+3  A: 

Perl modules don't have to be installed by an administrator. They can be located and run from anyhere, if you point Perl to the right location.

For modules which contain only Perl code (.pm) and no compiled code, this is as easy as uploading the .pm files in the right directory structure to your website.

Matthew Wilson
@Matthew Wilson: I think you are right, but I am also sure now that none of Template Toolkit, or HTML::Mason, or some other template / embed Perl modules can be installed by myself without enough privilege. Are there some .pm based modules that would let me finish off my task?
Michael Mao
I'm afraid I'm not an expert on any of these modules. Looking at your second update above though, you're getting permission errors because you're trying to install the modules globally. You don't have to do this. You can install them into a local area, if you can find the right magic to set this up. Have you tried just copying the directory tree containing the .pm files onto your server manually (forgetting about "make install" etc)?
Matthew Wilson
+5  A: 

Don't embed Perl into HTML. Use a template system like Template Toolkit or HTML::Template. They can be directly copied to server (if you don't use XS stash for TT) or download ports for this OS and unpack.

Alexandr Ciornii
+1 for template toolkit.
Philip Potter
@Alexandr Ciornii: I'd like to learn Template Toolkit or other template systems, but I've got limited time for this little project. I am very new to CGI programming with Perl so I would prefer to embed Perl into html like PHP, so I can spend more time to focus on the server-side business logic part.
Michael Mao
@Michael Mao: syntax of HTML::Template is very simple. Also look at Mojo::Template - it is using Perl for templates.
Alexandr Ciornii
+1  A: 

If you have compiler access, and access to make on the host machine, then you can use local::lib to abvoid having to have anything to do with the system perl.

singingfish
@singingfish: Sigh... This only lets me find out there are so many modules are either missing or too old there... very hard to upgrade everything(this is UNI, anyway, what can I do?) :(
Michael Mao
that doesn't really make sense. unless you're hard up on a disk quota, local::lib should just work. I like to use it with cpanminus to make things even quicker and easier
singingfish
+3  A: 

If you really need to embed perl within HTML then it might be worth having a look at Mojo::Template.

Its minimalistic and very straightforward Perl-ish template engine and is part of the Mojo project, which means even on a pristine Perl installation all you need to do is:

1. Download the source. Example using git (creates mojo folder in current directory):

git clone git://github.com/kraih/mojo.git

2. And use the Mojo library in your program. For eg:

#!/usr/bin/env perl
use strict;
use warnings;

use lib './mojo/lib';    # git clone here
use Mojo::Template;
my $mt = Mojo::Template->new;

print $mt->render_file( 'simple_template.html', 'Title text', 'Header text' );

with example template called simple_template.html:

<html>
% my ($title, $header) = @_;
<head>
    <title><%= $title %></title>
</head>
<body>
    <h1><%= $header %></h1>
    <ul>
        <% for my $i (1..5) { %>
            <li>item <%= $i %></li>
        <% } %>
    </ul>
</body>
</html>

This worked with no hitches for me on a freshly compiled perl 5.12.2.

NB. And don't forget you also get the full Mojo/Mojolicious web framework at no extra cost!


Disclaimer:

Like other answers here I generally steer clear of using embedded Perl HTML modules like Mojo::Template, Tenjin, HTML::Embperl et al. My preference has always been to go for more generic templating system like Template Toolkit.

However I have been moving more and more to HTML builder solutions and sometimes push style template modules like in these two SO question/answer:

/I3az/

draegtun