如何自动整理 Perl 源代码?
我公司的一只猫走过键盘,留下了 1000 多行有效的可执行 Perl 代码供我维护。
感谢 Perl 的 TMTOWTDI 哲学,我发现自己在 Google 上搜索以理解她生成的每一行代码。
更让我痛苦的是,代码没有缩进,并且人们发现这两个语句频繁出现在一行上,无法判断循环是外部还是内部。
我怎样才能自动让这个 Perl 代码变得理智?是的,我打赌会有一些 CPAN 模块可以做到这一点。一些外部工具怎么样?有什么线索吗?
A cat at my company walked over a keyboard and has left a valid 1000+ line of executable Perl code for me to maintain.
Thanks to Perl's TMTOWTDI philosophy I find myself searching Google to make sense of every line of code she has produced.
To add to my misery the code is not indented and one find frequent occurrence of the two statements on one line, inability figure out whether a loop is outer/inner.
How can I auto-intend this Perl code to sanity? Yes I bet there would be some CPAN module that does that. How about some external tool? Any clues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Perl::Tidy
可以做到这一点,甚至更多。它通常通过它安装的perltidy
可执行文件使用。Perl::Tidy
can do that, and much more. It's usually used through theperltidy
executable it installs.Perl Tidy 是一个非常有用的实用程序。它带有一系列令人讨厌的选项。
http://perltidy.sourceforge.net/ 和 http://perltidy.sourceforge.net/tutorial.html
例如
-i=8
覆盖数字缩进的空格(默认=4)和-bl
将大括号放在新行上:我建议播放代码副本并查看您最喜欢哪个选项。
您可以从 CPAN 安装它,也可以在 http://perltidy.sourceforge.net/ 上安装其他选项,具体取决于在您的平台和品味上!
Perl Tidy is a really useful utility. It comes with an offputting array of options.
There is some guidance at http://perltidy.sourceforge.net/ and http://perltidy.sourceforge.net/tutorial.html
For example
-i=8
overides the number of spaces to indent (default=4) and-bl
puts braces on a new line :I would suggest playing on a copy of the code and seeing which option you like the best.
You can either install it from CPAN, or varioujs other options on http://perltidy.sourceforge.net/ depending on your platform and taste !
以下是如何使用
perltidy
的一些示例非默认行为:启用 cuddle-elses(例如
} else {
),将所有 .pl 文件的行长度限制为 300 个字符<预><代码>$ perltidy -ce -l=300 *.pl
维护旧的逗号断点,冻结现有的空格脚本.pl
备份脚本并就地修改 script1.pl、script2.pl
通过去除尽可能多的空白来“混淆”脚本。
Here are a few examples of how to use
perltidy
with non-default behavior:Enable cuddled-elses ( eg.
} else {
), limit line lengths to 300 characters for all .pl filesMaintain old comma breakpoints, freeze existing whitespaces in script.pl
Backup script and modify script1.pl, script2.pl in-place
'Obfuscate' script by stripping it of as much whitespace as possible.
与大多数事情一样,如果您搜索 CPAN,您会更快找到答案比您登录 Stack Overflow 所需的时间要多。 :)
在本例中,正如其他人已经提到的,它是 Perl::Tidy 。我们在《有效的 Perl 编程》中也对此提供了一些较长的建议。
As with most things, if you search CPAN, you have your answer faster than it takes you to login to Stack Overflow. :)
In this case, it's Perl::Tidy as other people have already mentioned. We have some longer advice about this in Effective Perl Programming too.
您还可以使用在线工具 http://www.cleancss.com/perl-beautify/ 。
You can use also online tool http://www.cleancss.com/perl-beautify/ .