The easiest way to embed Perl in html

I researched online and found some interesting Perl modules / frameworks, such as HTML: Mason, HTML :: Embperl or the Catalyst MVC framework, etc. that can allow me to embed Perl inside html, like PHP code inside HTML.

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

For example, Apache version 1.3.3 and Perl version 5.8.0 (below Catalyst requirements)

I 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'm afraid none of them can let me embed Perl directly in html.

I know that I can use a simple print statement along with "heredoc" to print everything on an html page inside Perl / CGI, but I think this violates the MVC design paradigm and is less flexible and harder to develop, mainly because now business logic mixed up with html markup.

My current solution is to use jQuery to run AJAX requests to load html into specific tags from the client side. Thus, in this case, Perl is used only to provide access to server data, manage the relevant data, and provide JSON responses to AJAX requests.

I wonder if there is a better way to do this? I can hardly change the status of the server, and I don’t think that the system administrator will be so generous as to install any other Perl modules.

Updated information:

The main reason for embedding Perl in html is that I am very new to CGI programming, and since I am more familiar with PHP and jQuery, I would like to know if there is a suitable way to embed Perl directly in html, so I can quickly complete the client part and focus on server side.

Say something like this:

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

Please accept my little knowledge of Perl / CGI and thank you very much for your help in advance.

Updated 2: Following the instructions on the Template Toolkit website, I installed this module on my MacBook Pro, but unfortunately I can’t install it on a uni server due to permission:

 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'm now looking for other ways ...

Well, it looks like HTML :: Mason cannot be installed for the same reason. Therefore, I am afraid that I should find a solution .pm only so that I do not need to install anything in the perl uni server environment ...

+7
perl model-view-controller cgi
source share
4 answers

Perl modules should not be installed by the administrator. They can be located and run from anywhere if you point Perl to the right place.

For modules that contain only Perl code (.pm) and non-compiled code, it is as simple as uploading .pm files in the correct directory structure to your site.

+3
source share

Do not embed Perl in HTML. Use a template system such as the Template Toolkit or HTML :: Template. They can be directly copied to the server (if you do not use XS stash for TT) or download ports for this OS and unpack.

+5
source share

If you really need to embed perl in HTML, you might want to take a look at Mojo::Template .

Its minimalistic and very simple Perl-ish engine is part of the Mojo project, which means even on pristine Perl all you have to do is:

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

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

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

 #!/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 an example simple_template.html template:

 <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 without any clue for me on the recently compiled perl 5.12.2.

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


Disclaimer :

Like other answers, I generally avoid using the built-in HTML Perl modules, such as Mojo::Template , Tenjin , HTML::Embperl , etc. My preference was always to use a more general template system, such as the Template Toolkit .

However, I am moving more and more towards HTML linker solutions and sometimes click on template template modules, as in these two SO questions and answers:

  • CL-WHO HTML template for other languages?
  • Is there a small Perl XML parser that can replace the CGI.pm HTML generation functions?

/ I3az /

+4
source share

If you have access to the compiler and access to it on the host machine, you can use local :: lib to avoid having to have everything related to the perl system.

+1
source share

All Articles