simplest way to embed Perl in html
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 int开发者_C百科o 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...
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.
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:
- CL-WHO-like HTML templating for other languages?
- Is there a small Perl XML parser that can replace CGI.pm's HTML generation functions?
/I3az/
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.
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.
精彩评论