编写 Perl CGI 应用程序的最佳方法是什么?

发布于 2024-07-08 16:48:11 字数 380 浏览 6 评论 0 原文

我见过的每个 CGI/Perl 示例基本上都是一堆包含 HTML 的打印语句,这似乎不是编写 CGI 应用程序的最佳方法。 有一个更好的方法吗? 谢谢。

编辑:我决定使用 CGI::Application 和 HTML::Template,并使用以下教程: http://docs.google.com/View?docid=dd363fg9_77gb4hdh7b。 谢谢!

Every example I've seen of CGI/Perl basically a bunch of print statements containing HTML, and this doesn't seem like the best way to write a CGI app. Is there a better way to do this? Thanks.

EDIT: I've decided to use CGI::Application and HTML::Template, and use the following tutorial: http://docs.google.com/View?docid=dd363fg9_77gb4hdh7b. Thanks!

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

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

发布评论

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

评论(6

嘿哥们儿 2024-07-15 16:48:11

绝对可以(您可能正在看 90 年代的教程)。 您需要选择一个框架。 在 Perl 领域,这些是最流行的选择:

Absolutely (you're probably looking at tutorials from the 90s). You'll want to pick a framework. In Perl-land these are the most popular choices:

  • CGI::Application - very lightweight with lots of community plugins
  • Catalyst - heavier with more bells and whistles
  • Jifty - more magical than the above
守不住的情 2024-07-15 16:48:11

这是一个非常非常大的问题。 简而言之,更好的方法是 Model/View/Controller (又名 MVC )。 使用 MVC,您的应用程序被分为三个部分。

模型是您的数据和业务逻辑。 它是构成应用程序核心的东西。

视图是向用户呈现事物的代码。 在 Web 应用程序中,这通常是某种模板系统,但也可能是 PDF 或 Excel 电子表格。 基本上,这就是输出。

最后,你有了控制器。 它负责将模型和视图放在一起。 它接受用户的请求,获取相关的模型对象,并调用适当的视图。

mpeters 已经提到了几个 Perl 的 MVC 框架。 您还需要选择一个模板引擎。 最流行的两个是 模板工具包梅森

This is a really, really big question. In short, the better way is called Model/View/Controller (aka MVC). With MVC, your application is split into three pieces.

The Model is your data and business logic. It's the stuff that makes up the core of your application.

The View is the code for presenting things to the user. In a web application, this will typically be some sort of templating system, but it could also be a PDF or Excel spreadsheet. Basically, it's the output.

Finally, you have the Controller. This is responsible for putting the Model and View together. It takes a user's request, gets the relevant model objects, and calls the appropriate view.

mpeters already mentioned several MVC frameworks for Perl. You'll also want to pick a templating engine. The two most popular are Template Toolkit and Mason.

笑看君怀她人 2024-07-15 16:48:11

暂时留下 CGI 与 MVC 框架的问题,您需要的是 CPAN 的输出模板模块之一。

模板工具包非常流行(CPAN 上的 Template.pm)
同样流行的还有 Text::Template、HTML::Template 和 HTML::Mason。

HTML::Mason 不仅仅是一个模板模块,因此对于一个简单的 CGI 应用程序来说可能有点太重了,但在您决定哪个最适合您时值得研究一下。

Text::Template 相当简单,并且在模板中使用 Perl,因此您可以循环数据并在 Perl 中执行显示逻辑。 人们认为这既有利又不利。

HTML::Template 也小而简单。 它实现了自己的一小组标签,用于 if/then/else 处理、变量设置和循环。 就是这样。 出于与 Text::Template 完全相反的原因,这被视为有利有弊。

模板工具包 (TT) 实现了一种非常庞大、糟糕的模板语言,其中包括循环和逻辑等等。

我使用了 HTML::Template 之一,发现我想要更多的功能。 然后我成功地使用了 Text::Template,但发现它想要摆弄名称空间的愿望有点烦人。 我开始了解并喜欢模板工具包。 对我来说,这感觉不错。
你的旅费可能会改变。

当然,仍然有旧的“打印 HTML”方法,有时几条打印语句就足够了。 但是您突然想到将显示与主要逻辑分开的想法。 这是一件好事。

这是通往模型/视图/控制器 (MVC) 的第一步,其中您将数据模型和业务逻辑(接受输入、对其执行某些操作并决定需要输出的内容的代码)分开,您的您的输入/输出(模板或打印语句 - HTML、PDF 等),以及连接两者的代码(CGI、CGI::Application、Catalyst MVC 框架等)。 这个想法是,对数据结构(模型中)的更改不应要求更改输出例程(视图)。

Leaving the question of CGI vs MVC framework for the moment, what you're going to want is one of the output templating modules from the CPAN.

The Template Toolkit is very popular (Template.pm on CPAN)
Also popular are Text::Template, HTML::Template, and HTML::Mason.

HTML::Mason is much more than a template module, and as such might be a little too heavy for a simple CGI app, but is worth investigating a little while you're deciding which would be best for you.

Text::Template is reasonably simple, and uses Perl inside the templates, so you can loop over data and perform display logic in Perl. This is seen as both a pro and con by people.

HTML::Template is also small and simple. It implements its own small set of tags for if/then/else processing, variable setting, and looping. That's it. This is seen as both a pro and a con for the exact opposite reasons as Text::Template.

Template toolkit (TT) implements a very large, perlish template language that includes looping and logic, and much more.

I used HTML::Template one, and found I wanted a few more features. I then used Text::Template with success, but found its desire to twiddle with namespaces to be a little annoying. I've come to know and love Template Toolkit. For me it just feels right.
Your mileage may vary.

Of course, there is still the old "print HTML" method, sometimes a couple of print statements suffices. But you've hit upon the idea of separating your display from your main logic. Which is a good thing.

It's the first step down the road to Model/View/Controller (MVC) in which you keep separate your data model&business logic (your code that accepts the input, does something with it, and decides what needs to be output), your your input/output (Templates or print statements - HTML, PDF, etc.) , and the code that connects the two (CGI, CGI::Application, Catalyst MVC Framework, etc.). The idea being that a change to your data structure (in the Model) should not require changes to your output routines (View).

拒绝两难 2024-07-15 16:48:11

Perl5 Wiki 提供了一个很好的(尽管还不完整)列表: href="http://www.perlfoundation.org/perl5/index.cgi?web_frameworks" rel="nofollow noreferrer">Web 框架 & 模板

“模板”wiki 条目中链接的比较文章值得一读。 我还建议阅读这篇关于 PerlMonks 的 推送样式模板系统 文章。

对于模板,Template Toolkit 是我最常用的工具包,强烈推荐它。 还有一本 O'Reilly 书,它可能是最常用的模板系统Perl 王国(Web 框架内部或外部)。

我越来越感兴趣的另一种方法是非模板“构建器”解决方案。 Template::Declare 等模块 HTML::AsSubs 符合这个要求。

The Perl5 Wiki provides a good (though not yet complete) list of web frameworks & templates.

The comparison articles linked in the "templates" wiki entry is worth reading. I would also recommend reading this push style templating systems article on PerlMonks.

For templating then Template Toolkit is the one I've used most and can highly recommend it. There is also an O'Reilly book and is probably the most used template system in the Perl kingdom (inside or outside of web frameworks).

Another approach which I've been drawn more and more to is non template "builder" solutions. Modules like Template::Declare & HTML::AsSubs fit this bill.

说谎友 2024-07-15 16:48:11

我认为在 Framework/Roll-your-own 困境中取得适当平衡的一个解决方案是使用三个关键的 Perl 模块: CGI.pm, 模板工具包DBI。 通过这三个模块,您可以进行优雅的、易于编写和维护的 MVC 编程。

所有三个模块都非常灵活,模板工具包 (TT) 允许您以 html、XML 甚至 pdf 格式输出数据(如果需要)。 您还可以在 TT 中包含 Perl 逻辑,甚至在其中添加数据库接口。 这使得您的 CGI 脚本非常小并且易于维护,特别是当您使用“标准”编译指示时。

它还允许您将 JavaScript 和 AJAXy 内容放入模板本身中,从而模拟客户端和服务器之间的分离。

这三个模块是 CPAN 上最受欢迎的模块,拥有优秀的文档和广泛的用户群。 另外,使用这些进行开发意味着一旦您完成了代码原型,您就可以很快地转向 mod_perl,从而快速过渡到 Apache 的嵌入式 perl 环境。

总而言之,这是一个紧凑而灵活的工具集。

One solution that I feel strikes the right balance in the Framework/Roll-your-own dilemma is the use of three key perl modules: CGI.pm, Template Toolkit , and DBI. With these three modules you can do elegant MVC programming that is easy to write and to maintain.

All three modules are very flexible with Template Toolkit (TT) allowing you to output data in html, XML, or even pdf if you need to. You can also include perl logic in TT, even add your database interface there. This makes your CGI scripts very small and easy to maintain, especially when you use the "standard" pragma.

It also allows you to put JavaScript and AJAXy stuff in the template itself which mimics the separation between client and server.

These three modules are among the most popular on CPAN, have excellent documentation and a broad user base. Plus developing with these means you can rather quickly move to mod_perl once you have prototyped your code giving you a quick transition to Apache's embedded perl environment.

All in all a compact yet flexible toolset.

北陌 2024-07-15 16:48:11

您还可以将演示与代码分开,只使用模板系统,而​​无需引入成熟框架的所有开销。 模板工具包可以以这种方式单独使用,Mason,尽管我倾向于认为它更像是一个伪装成模板系统的框架。

不过,如果您真的热衷于将代码与表示分离,请注意 TT 和 Mason 都允许(甚至鼓励,取决于您阅读的文档)将可执行代码嵌入到模板中。 就我个人而言,我觉得在 HTML 中嵌入代码并不比在代码中嵌入 HTML 更好,所以我倾向于选择 HTML::模板

You can also separate presentation out from code and just use a templating system without needing to bring in all the overhead of a full-blown framework. Template Toolkit can be used by itself in this fashion, as can Mason, although I tend to consider it to be more of a framework disguised as a templating system.

If you're really gung-ho about separating code from presentation, though, be aware that TT and Mason both allow (or even encourage, depending on which docs you read) executable code to be embedded in the templates. Personally, I feel that embedding code in your HTML is no better than embedding HTML in your code, so I tend to opt for HTML::Template.

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