我怎样才能学会用 Perl 编写结构良好的程序? [英] How can I learn to write well-structured programs in Perl?

查看:43
本文介绍了我怎样才能学会用 Perl 编写结构良好的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习 Perl,但我以前的大部分编程经验都是使用强调面向对象编程的语言,例如 C# 和 Java.我发现的所有 Perl 示例往往都是很长的单功能程序,而且我发现我自己编写代码的方式也是如此.是否有任何资源或教程可以编写可维护且结构良好的程序?

I've started learning Perl but most of my former programming experience has been in languages that emphasize object oriented programming such as C# and Java. All the Perl examples I have found tend to be long single function programs and I find my self writing code the same way. Are there any resources or tutorials for writing maintainable well structured programs?

推荐答案

首先,无论您在做什么类型的 Perl 编程,您都可能会发现 Perl::Critic 是无价的.命令行工具是迄今为止获取代码反馈最方便的工具,但您也可以使用网络界面您可以在其中上传 Perl 代码并接收即时、自动的反馈.请注意,Perl::Critic 不会教您良好的结构,但它有助于改善您的风格,并引导您远离一些常见错误.

Firstly, regardless of what sort of Perl programming you're doing, you'll probably find Perl::Critic to be invaluable. The command-line tool is by far the most convenient for getting feedback on your code, but you there is also a web interface where you can upload your Perl code and receive instant, automated feedback. Note that Perl::Critic isn't going to teach you good structure, but it will help improve your style in general, and steer you away from some common mistakes.

要使用 Perl::Critic,我建议您获取一份 Perl Best Practices(PBP).它包含许多 Perl::Critic 所基于的详细信息.即使您不同意书中的特定指南,它也会让您思考如何编码,这是非常非常有价值的.你不必为一本书掏钱,但两者相互赞美非常好,而且你会在 PBP 中找到冗长的讨论,而你不会从 Perl::Critic 那里得到.

To go with Perl::Critic, I'd recommend getting a copy of Perl Best Practices (PBP). It contains a lot of detailed information upon which Perl::Critic was based. Even if you disagree with particular guidelines in the book, it makes you think about how you code, and that's very, very valuable. You don't have to shell out money for a book, but the two compliment each other very nicely, and there are lengthy discussions you'll find in PBP that you won't get from Perl::Critic.

如果您已经使用过其他面向对象语言和面向对象设计,您可能会发现 Moose成为一个舒适的过渡.Moose 非常稳定,得到很好的支持,并且拥有庞大而活跃的社区(尤其是通过 IRC).Moose 取代了几乎所有现有的 OO Perl 建议,包括我自己的.面向对象的设计很常见是有原因的.它对很多项目都有意义,而且没有理由在 Perl 中使用它.

If you've already worked with other OO languages and OO design, the you'll probably find Moose to be a comfortable transition. Moose is very stable, very well supported, and has a huge and active community (especially via IRC). Moose supersedes almost all the existing OO Perl advice out there, including my own. Object Oriented design is common for a reason; it makes sense for a lot of projects, and there's no reason not to use it in Perl.

就我个人而言,当我转向测试驱动的开发模型时,我发现自己的程序结构有了很大的改进.在这样的模型下,将问题分解为易于测试的小单元是必不可少的.如果您不熟悉 Perl 测试,请从 Test::Tutorial 开始,然后查看在其他一些测试资源书籍 如果您想了解更多信息.使用像 Devel::CoverDevel::NYTProf 看看你的测试用例是什么,什么不是.代码难以测试通常是结构不佳的标志.

Personally, I found a massive improvement in my own program structure when I moved to a test-driven development model. Under such a model, breaking a problem down into small, easily tested units is essential. Start with Test::Tutorial if you're new to testing in Perl and then look at some other testing resources or books if you want to learn more. Use a tool like Devel::Cover or Devel::NYTProf to see what your test cases are hitting and what they're not. Having code that's hard to test is often a sign of poor structure.

说了这么多,迄今为止最好的老师是与经验丰富的贡献者一起参与现有的 Perl 项目.看看他们是怎么做事的,当你做出贡献时,想想他们的建议.如果你想要一个真正的应用程序,它似乎已经吸收了 Perl 社区中最优秀和最聪明的人的强大价值,那么我建议你参与 Padre,Perl 编辑器.

Having said all this, the best teacher by far is to get involved with an existing Perl project with experienced contributors. See how they do things, and when you make contributions, think about their advice. If you want a real application with awesome cool value that seems to have sucked up the very best and brightest of the Perl community, then I'd recommend getting involved with Padre, the Perl editor.

如果(无论出于何种原因)您无法参与另一个项目,请考虑将代码示例发布到社区,例如 Stack Overflow 或 PerlMonks.更好的是,如果你能让你的代码开源,那么就这样做,并征求反馈.所有编程语言都可以与已经熟悉它们的人一起学习,Perl 也不例外.

If (for whatever reason) you can't get involved with another project, then consider posting code samples to communities such as Stack Overflow, or PerlMonks. Better still, if you can make your code open source, then do so, and solicit feedback. All programming languages are better learnt with others who are already familiar with them, and Perl is no exception here.

愿你用 Perl 施展魔法

May you do Good Magic with Perl,

保罗

这篇关于我怎样才能学会用 Perl 编写结构良好的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆