我应该使用面向函数还是面向对象的 CGI 接口?

发布于 2024-09-17 17:55:21 字数 119 浏览 3 评论 0原文

我最近一直在学习 CGI 模块,我正在使用的书显示有两种使用 CGI 的方法,面向函数或面向对象。他们说面向对象的好处只是能够创建两个 CGI 对象。首先这是真的吗?还有其他好处吗?其次有什么使用两个 CGI 对象的例子吗?

I've been learning about the CGI module lately, and the book I'm using shows there are two ways you can use CGI, function-oriented or object-oriented. They say the benefit of having object-oriented is only to be able to create two CGI objects. First of all is this true, and are there any other benefits, and secondly what example is there for using two CGI objects?

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

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

发布评论

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

评论(3

婴鹅 2024-09-24 17:55:21

当我需要组合一个非常简单的 CGI 脚本时,我使用 CGI 模块的 OO 接口。

我使用 OOP 接口是因为标准的命令式接口导入了大量可能与我自己的符号冲突的符号。我不喜欢这样,所以我总是阻止符号导入。我不使用 CGI;。相反,我使用 CGI ();

我还将我的用途限制为生成标头和解析参数。我总是将 HTML 生成为 HTML,或者更好的是,使用 TemplateToolkit 等模板模块。

我严格避免CGI 的HTML 生成功能。为什么?

  • 我(和许多其他人一样)已经了解 HTML,并且我认为学习 CGI 的伪 html 界面没有任何好处。
  • 当脚本长大并需要在另一个环境中使用时,提取 HTML 块或模板并重用它们会更容易。

不要将我所写的内容理解为对 CGI.pm 的全面谴责。 CGI.pm 有很多值得喜爱的地方。它可以正确生成内容类型。它使参数解析变得微不足道。它是一个核心模块。它使命令行调试和测试变得容易。

When I need to put together a very simple CGI script, I use the CGI module's OO interface.

I use the OOP interface because the standard, imperative interface imports a ton of symbols that may conflict with my own symbols. I don't like this, so I always prevent symbol importation. I don't use CGI;. Instead, I use CGI ();.

I also limit my use to generating the header and parsing parameters. I always generate HTML as HTML or better yet, use a template module like TemplateToolkit.

I strictly avoid CGI's HTML generation functions. Why?

  • I (along with many other people) already know HTML, and I see no benefit in learning CGI's pseudo-html interface.
  • When a script grows up and needs to be used in another environment, it is easier to extract the HTML blocks or templates and reuse them.

Don't interpret what I've written as a blanket condemnation of CGI.pm. There's plenty to love about CGI.pm. It gets content type generation right. It makes parameter parsing trivial. It is a core module. It makes command line debugging and testing easy.

岁月流歌 2024-09-24 17:55:21

我想我已经找到了我的问题的答案

http://perldoc.perl.org/ CGI.html#PROGRAMMING-STYLE

阅读常见问题解答,给出了多次使用 CGI 对象的示例,我可以存储 CGI 并加载以前的 CGI 对象,这非常有用。

I think I have found the answer to my question

http://perldoc.perl.org/CGI.html#PROGRAMMING-STYLE

Reading through the faq, an example given for multiple uses of CGI objects is I can store CGI and load previous CGI objects, which is quite useful.

过潦 2024-09-24 17:55:21

除了您引用的优点之外,我还想指出,CGI.pm 的 OOP 使用比功能版本更易于阅读(至少对我而言)和管理。

我还怀疑它更常见,因此在您之后必须维护代码的人(包括从现在起六个月后的您)会发现维护起来更容易。

Beyond the advantages you cite I'd also point out that OOP usage of CGI.pm is much cleaner to read (at least for me) and manage than the functional version.

I also suspect it is more common so people who have to maintain your code after you (including you six months from now) will find it easier to maintain.

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