我应该使用面向函数还是面向对象的 CGI 接口?
我最近一直在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我需要组合一个非常简单的 CGI 脚本时,我使用 CGI 模块的 OO 接口。
我使用 OOP 接口是因为标准的命令式接口导入了大量可能与我自己的符号冲突的符号。我不喜欢这样,所以我总是阻止符号导入。我不
使用 CGI;
。相反,我使用 CGI ();
。我还将我的用途限制为生成标头和解析参数。我总是将 HTML 生成为 HTML,或者更好的是,使用 TemplateToolkit 等模板模块。
我严格避免CGI 的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, Iuse 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?
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.
我想我已经找到了我的问题的答案
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.
除了您引用的优点之外,我还想指出,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.