启用全局警告
我必须优化一个用 Perl 编写的 Intranet(大约 3000 个文件)。我想做的第一件事是启用警告“-w
”或“use warnings;
”,这样我就可以摆脱所有这些错误,然后尝试实现“<代码>使用严格;”。
有没有一种方法可以告诉 Perl 始终使用警告(例如 PHP 的 php.ini 中的设置),而不需要修改每个脚本以将“-w”添加到其第一行?
我什至想为 /usr/bin/perl
创建一个别名,或者将其移至另一个名称并创建一个简单的脚本,而不是仅仅添加 -w
标志(就像代理一样)。
你会如何调试它?
I have to optimize an intranet written in Perl (about 3000 files). The first thing I want to do is enable warnings "-w
" or "use warnings;
" so I can get rid of all those errors, then try to implement "use strict;
".
Is there a way of telling Perl to use warnings all the time (like the settings in php.ini
for PHP), without the need to modify each script to add "-w" to it's first line?
I even thought to make an alias for /usr/bin/perl
, or move it to another name and make a simple script instead of it just to add the -w
flag (like a proxy).
How would you debug it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯……
您可以设置 PERL5OPT 变量来保存
-w
。有关详细信息,请参阅 perlrun 联机帮助页。我希望您也考虑使用污染(例如-T
或-t
)来进行安全跟踪。但我不羡慕你。在没有使用警告和使用严格的情况下开发的代码改造通常是皇家 PITA。
我有一些标准样板我用来启动新的 Perl 程序。但我还没有考虑过 CGI 程序,它可能会从针对样板的一些调整中受益。
Well…
You could set the PERL5OPT envariable to hold
-w
. See the perlrun manpage for details. I hope you’ll consider tainting, too, like-T
or maybe-t
, for security tracking.But I don’t envy you. Retrofitting code developed without the benefit of
use warnings
anduse strict
is usually a royal PITA.I have something of a standard boiler-plate I use to start new Perl programs. But I haven’t given any thought to one for CGI programs, which would likely benefit from some tweaks against that boiler-plate.
改造警告和严格是很困难的。我不推荐大爆炸的方法,对所有事情都设置警告(更不用说限制)。你将会被淹没在毫无用处的警告之中。
首先对脚本使用的模块启用警告(有一些,不是吗?),而不是对所有内容应用警告。将核心清理干净,然后开始处理外围,一次一个单元。因此,事实上,我建议使用一个简单的 (Perl) 脚本,该脚本只需查找不以哈希开头的行并添加
use warnings;
(也许还可以use strict;< /code> 也是如此,因为您将一次处理一个脚本),因此您可以一次只处理一个脚本。
换句话说,您最好在要更新每个文件时实际编辑它。
我只会使用一揽子选项来对问题的范围进行简单的评估:这是一场彻底的灾难,还是仅仅是几个文件中的一些过失。可悲的是,如果代码是在没有警告和严格的情况下开发的,那么它更有可能是“灾难”而不是“最小”。
您可能会发现您的前辈很容易复制和粘贴,并且在复制的代码中反复出现一些错误的习惯用法。编写一个 Perl 脚本来修复每个问题。我的个人
bin
目录中有一堆fix*
脚本,用于处理各种更改 - 修复由顽固的(或者更常见的是,早已离开的)同事创建的问题或者适应我自己不断变化的标准。Retrofitting warnings and strict is hard. I don't recommend a Big Bang approach, setting warnings (let alone strictures) on everything. You will be inundated with warnings to the point of uselessness.
You start by enabling warnings on the modules used by the scripts (there are some, aren't there?), rather than applying warnings to everything. Get the core clean, then get to work on the periphery, one unit at a time. So, in fact, I'd recommend having a simple (Perl) script that simply finds a line that does not start with a hash and adds
use warnings;
(and maybeuse strict;
too, since you're going to be dealing with one script at a time), so you can do the renovations one script at a time.In other words, you will probably be best off actually editing each file as you're about to renovate it.
I'd only use the blanket option to make a simple assessment of the scope of the problem: is it a complete and utter disaster, or merely a few peccadilloes in a few files. Sadly, if the code was developed without warnings and strict, it is more likely to be 'disaster' than 'minimal'.
You may find that your predecessors were prone to copy and paste and some erroneous idioms crop up repeatedly in copied code. Write a Perl script that fixes each one. I have a bunch of
fix*
scripts in my personalbin
directory that deal with various changes - either fixing issues created by recalcitrant (or, more usually, simply long departed) colleagues or to accommodate my own changing standards.您可以通过将
-Mwarnings -Mstrict
添加到PERL5OPT
环境变量来为所有 Perl 脚本设置警告和限制。有关详细信息,请参阅perlrun
。You can set warnings and strictures for all Perl scripts by adding
-Mwarnings -Mstrict
to yourPERL5OPT
environment variable. Seeperlrun
for details.