在 html 中嵌入 Perl 的最简单方法

发布于 2024-09-16 23:49:08 字数 1831 浏览 9 评论 0原文

我在网上研究了一下,发现了几个有趣的 Perl 模块/框架,例如 HTML:Mason、HTML::Embperl 或 MVC Catalyst 框架等,它们可以让我将 Perl 嵌入到 html 中,就像将 PHP 代码嵌入到 html 中一样。

但是,我的 Perl 项目必须上传到 uni 服务器,该服务器仅提供有限的权限和资源。

例如,Apache 版本 1.3.3 & Perl 版本 5.8.0(低于 Catalyst 的要求)

我使用了一个脚本来检查所有已安装的 Perl 模块,只有那些名称包含单词“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

恐怕它们都不能让我将 Perl 直接嵌入到 html 中。

我知道我可以使用简单的 print 语句和“heredoc”在 Perl/CGI 中打印 html 页面上的所有内容,但我认为这违反了 MVC 设计范式,并且灵活性较差,开发起来更复杂,主要是因为现在业务逻辑被 html 标记搞乱了。

我当前的解决方案是使用 jQuery 触发 AJAX 请求从客户端将 html 加载到特定标签中。因此,在这种情况下,Perl 仅用于提供服务器端数据访问、操作相关数据并为 AJAX 请求提供 JSON 格式的响应。

我想知道有更好的方法吗?我几乎无法更改服务器状态,而且我认为系统管理员不会那么慷慨地安装任何其他 Perl 模块。

更新信息:

将Perl嵌入到html中的主要原因是我对CGI编程非常陌生,并且由于我更熟悉PHP和jQuery,我想知道是否有合适的将 Perl 直接嵌入到 html 中的方法,这样我就可以很快完成客户端部分并专注于服务器端。

请像这样说:

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

请容忍我对 Perl/CGI 的一点了解,并非常感谢提前提供的帮助。

第二次更新: 按照Template Toolkit网站的说明,我在自己的MacBook Pro上安装了这个模块,但不幸的是由于权限原因我无法将其安装到uni的服务器上:

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

所以不幸的是我现在正在寻找其他方法...

好吧,看来HTML::Mason由于同样的原因无法安装。因此,我担心我必须找到一个仅 .pm 的解决方案,这样我就不必在 uni 服务器的 perl 环境中安装任何东西......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ま昔日黯然 2024-09-23 23:49:08

不要将 Perl 嵌入到 HTML 中。使用模板系统,如 Template Toolkit 或 HTML::Template。它们可以直接复制到服务器(如果您不使用 XS stash 进行 TT)或下载此操作系统的端口并解压。

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.

筱武穆 2024-09-23 23:49:08

如果您确实需要在 HTML 中嵌入 perl,那么可能值得一看Mojo::Template

它简约且非常简单的 Perl-ish 模板引擎,是 Mojo 项目,这意味着即使在原始 Perl 安装上,您所需要做的就是:

1.下载源代码。使用 git 的示例(在当前目录中创建 mojo 文件夹):

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

2.并在程序中使用 Mojo 库。 例如:

#!/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' );

使用名为 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>

这对我来说在新编译的 perl 5.12.2 上没有任何问题。

注意。不要忘记,您还可以免费获得完整的 Mojo/Mojolicious Web 框架!

< br>
免责声明

像这里的其他答案一样,我通常避免使用嵌入式 Perl HTML 模块,例如 Mojo::Template, 天神HTML::Embperl 等人。我一直倾向于选择更通用的模板系统,例如 Template Toolkit

然而,我已经越来越多地转向 HTML 构建器解决方案,有时推送样式模板模块,如这两个 SO 问题/答案:

/I3az/

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/

女皇必胜 2024-09-23 23:49:08

Perl 模块不必由管理员安装。如果您将 Perl 指向正确的位置,则可以从任何地方找到它们并运行它们。

对于仅包含 Perl 代码 (.pm) 而没有编译代码的模块,这就像将正确目录结构中的 .pm 文件上传到您的网站一样简单。

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.

囍笑 2024-09-23 23:49:08

如果您具有编译器访问权限,并且可以在主机上进行 make 访问,那么您可以使用 local: :lib 以避免与系统 perl 有任何关系。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文