如何检查Perl-CGI应用程序中的所有错误(例如,使用Perl编译器或类似lint的工具检查,免费开源?)

时间:2011-06-29 08:32:49

标签: perl compilation execution runtime-error lint

我有一个关键任务Perl-CGI服务器端应用程序,我需要不时扩展或增强。

是否有工具可以检查应用程序的所有执行路径,即所有Perl文件,所有函数,所有if-else,switch-case等可能性。

作为一种解释型语言,在修改代码期间引入的错误并不总是变得明显。如果一个人认为一切正常,可能会被误导,如果在做了更改后,Perl程序在做了一些测试后运行正常。

全面的测试套件可能是一个解决方案,但是在时间紧缺的情况下,这总是会对程序进行自定义并需要额外的时间来实施。

相反,我认为一个有效的工具对于“毯子”练习/解析/任何每个执行路径都是通用的,例如:检查未声明的变量,更改为函数/子例程名称,例如由于重构等。

我有ActiveState Komodo IDE,它在检测错误方面做得很好但是作为商业工具,我不会总是能够在我工作的所有环境中使用它。所以我更喜欢免费和开源的东西。

更新

感谢@Nylon Smile for Perl ::评论 - 下面的答案 - 我在Perl设置中没有标准,所以不得不使用cpan来安装它,当然:)

我在详细的安装进度中看到了很多成功(见下文),但也有一些失败:(还有“为Perl-Critic-1.116提供安装”这看起来并不好看:(

每当我尝试安装或升级Perl模块时,这种情况并不少见。我有CentOS 5。

这给我的印象是维护,配置,升级Perl是一个相当混乱的事情,非常狡猾,可能与“但你没有”和“if-you”关于你必须做什么让它工作:)

[root]# cpanm Perl::Critic
--> Working on Perl::Critic
Fetching http://search.cpan.org/CPAN/authors/id/E/EL/ELLIOTJS/Perl-Critic-1.116.tar.gz ... OK
Configuring Perl-Critic-1.116 ... OK
==> Found dependencies: PPI, B::Keywords, PPI::Document, List::MoreUtils, PPIx::Utilities::Statement, PPI::Token::Whitespace,        PPI::Node, Pod::Spell, PPI::Token::Quote::Single, Perl::Tidy, String::Format, PPI::Document::File, Task::Weaken, Module::Plugg       able, IO::String, version, PPIx::Utilities::Node, Readonly, Exporter, Exception::Class, PPIx::Regexp, Config::Tiny, Email::Add       ress, Test::Deep
--> Working on PPI
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/PPI-1.215.tar.gz ... OK
Configuring PPI-1.215 ... OK
==> Found dependencies: File::Remove, Test::Object, Storable, Test::NoWarnings, Params::Util, Test::SubCalls, Task::Weaken, Li       st::MoreUtils, IO::String, Clone, Class::Inspector
--> Working on File::Remove
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/File-Remove-1.49.tar.gz ... OK
Configuring File-Remove-1.49 ... OK
==> Found dependencies: File::Spec, Cwd
--> Working on File::Spec
Fetching http://search.cpan.org/CPAN/authors/id/S/SM/SMUELLER/PathTools-3.33.tar.gz ... OK
Configuring PathTools-3.33 ... OK
Building and testing PathTools-3.33 ... FAIL
! Installing File::Spec failed. See /root/.cpanm/build.log for details.
! Bailing out the installation for File-Remove-1.49. Retry with --prompt or --force.
--> Working on Test::Object
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/Test-Object-0.07.tar.gz ... OK
Configuring Test-Object-0.07 ... OK
Building and testing Test-Object-0.07 ... OK
Successfully installed Test-Object-0.07
--> Working on Storable
Fetching http://search.cpan.org/CPAN/authors/id/A/AM/AMS/Storable-2.25.tar.gz ... OK
Configuring Storable-2.25 ... OK
Building and testing Storable-2.25 ... FAIL
! Installing Storable failed. See /root/.cpanm/build.log for details.
--> Working on Test::NoWarnings
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/Test-NoWarnings-1.02.tar.gz ... OK
Configuring Test-NoWarnings-1.02 ... OK
==> Found dependencies: Test::Tester
--> Working on Test::Tester
Fetching http://search.cpan.org/CPAN/authors/id/F/FD/FDALY/Test-Tester-0.107.tar.gz ... OK
Configuring Test-Tester-0.107 ... OK
Building and testing Test-Tester-0.107 ... OK
Successfully installed Test-Tester-0.107
Building and testing Test-NoWarnings-1.02 ... OK
Successfully installed Test-NoWarnings-1.02
--> Working on Params::Util
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/Params-Util-1.04.tar.gz ... OK
Configuring Params-Util-1.04 ... OK
Building and testing Params-Util-1.04 ... OK
Successfully installed Params-Util-1.04
--> Working on Test::SubCalls
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/Test-SubCalls-1.09.tar.gz ... OK
Configuring Test-SubCalls-1.09 ... OK
==> Found dependencies: Hook::LexWrap
--> Working on Hook::LexWrap
Fetching http://search.cpan.org/CPAN/authors/id/C/CH/CHORNY/Hook-LexWrap-0.24.tar.gz ... OK
Configuring Hook-LexWrap-0.24 ... OK
Building and testing Hook-LexWrap-0.24 ... OK
Successfully installed Hook-LexWrap-0.24
Building and testing Test-SubCalls-1.09 ... OK
Successfully installed Test-SubCalls-1.09
--> Working on Task::Weaken
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/Task-Weaken-1.04.tar.gz ... OK
Configuring Task-Weaken-1.04 ... OK
Building and testing Task-Weaken-1.04 ... OK
Successfully installed Task-Weaken-1.04
--> Working on List::MoreUtils
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/List-MoreUtils-0.32.tar.gz ... OK
Configuring List-MoreUtils-0.32 ... OK
Building and testing List-MoreUtils-0.32 ... FAIL
! Installing List::MoreUtils failed. See /root/.cpanm/build.log for details.
--> Working on IO::String
Fetching http://search.cpan.org/CPAN/authors/id/G/GA/GAAS/IO-String-1.08.tar.gz ... OK
Configuring IO-String-1.08 ... OK
Building and testing IO-String-1.08 ... OK
Successfully installed IO-String-1.08
--> Working on Clone
Fetching http://search.cpan.org/CPAN/authors/id/R/RD/RDF/Clone-0.31.tar.gz ... OK
Configuring Clone-0.31 ... OK
Building and testing Clone-0.31 ... FAIL
! Installing Clone failed. See /root/.cpanm/build.log for details.
--> Working on Class::Inspector
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/Class-Inspector-1.25.tar.gz ... OK
Configuring Class-Inspector-1.25 ... OK
Building and testing Class-Inspector-1.25 ... OK
Successfully installed Class-Inspector-1.25
! Bailing out the installation for PPI-1.215. Retry with --prompt or --force.
--> Working on B::Keywords
Fetching http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/B-Keywords-1.10.tar.gz ... OK
Configuring B-Keywords-1.10 ... OK
Building and testing B-Keywords-1.10 ... OK
Successfully installed B-Keywords-1.10
--> Working on PPIx::Utilities::Statement
Fetching http://search.cpan.org/CPAN/authors/id/E/EL/ELLIOTJS/PPIx-Utilities-1.001000.tar.gz ... OK
Configuring PPIx-Utilities-1.001000 ... OK
==> Found dependencies: PPI, Readonly, Exception::Class, PPI::Document::Fragment, PPI::Dumper, PPI::Document, Test::Deep
--> Working on Readonly
Fetching http://search.cpan.org/CPAN/authors/id/R/RO/ROODE/Readonly-1.03.tar.gz ... OK
Configuring Readonly-1.03 ... OK
Building and testing Readonly-1.03 ... OK
Successfully installed Readonly-1.03
--> Working on Exception::Class
Fetching http://search.cpan.org/CPAN/authors/id/D/DR/DROLSKY/Exception-Class-1.32.tar.gz ... OK
Configuring Exception-Class-1.32 ... OK
==> Found dependencies: Devel::StackTrace, Class::Data::Inheritable
--> Working on Devel::StackTrace
Fetching http://search.cpan.org/CPAN/authors/id/D/DR/DROLSKY/Devel-StackTrace-1.27.tar.gz ... OK
Configuring Devel-StackTrace-1.27 ... OK
Building and testing Devel-StackTrace-1.27 ... OK
Successfully installed Devel-StackTrace-1.27
--> Working on Class::Data::Inheritable
Fetching http://search.cpan.org/CPAN/authors/id/T/TM/TMTM/Class-Data-Inheritable-0.08.tar.gz ... OK
Configuring Class-Data-Inheritable-0.08 ... OK
Building and testing Class-Data-Inheritable-0.08 ... OK
Successfully installed Class-Data-Inheritable-0.08
Building and testing Exception-Class-1.32 ... OK
Successfully installed Exception-Class-1.32
--> Working on Test::Deep
Fetching http://search.cpan.org/CPAN/authors/id/R/RJ/RJBS/Test-Deep-0.108.tar.gz ... OK
Configuring Test-Deep-0.108 ... OK
Building and testing Test-Deep-0.108 ... OK
Successfully installed Test-Deep-0.108
Building and testing PPIx-Utilities-1.001000 ... FAIL
! Installing PPIx::Utilities::Statement failed. See /root/.cpanm/build.log for details.
--> Working on Pod::Spell
Fetching http://search.cpan.org/CPAN/authors/id/S/SB/SBURKE/Pod-Spell-1.01.tar.gz ... OK
Configuring Pod-Spell-1.01 ... OK
Building and testing Pod-Spell-1.01 ... OK
Successfully installed Pod-Spell-1.01
--> Working on Perl::Tidy
Fetching http://search.cpan.org/CPAN/authors/id/S/SH/SHANCOCK/Perl-Tidy-20101217.tar.gz ... OK
Configuring Perl-Tidy-20101217 ... OK
Building and testing Perl-Tidy-20101217 ... OK
Successfully installed Perl-Tidy-20101217
--> Working on String::Format
Fetching http://search.cpan.org/CPAN/authors/id/D/DA/DARREN/String-Format-1.16.tar.gz ... OK
Configuring String-Format-1.16 ... OK
Building and testing String-Format-1.16 ... OK
Successfully installed String-Format-1.16
--> Working on Module::Pluggable
Fetching http://search.cpan.org/CPAN/authors/id/S/SI/SIMONW/Module-Pluggable-3.9.tar.gz ... OK
Configuring Module-Pluggable-3.9 ... OK
Building and testing Module-Pluggable-3.9 ... OK
Successfully installed Module-Pluggable-3.9
--> Working on version
Fetching http://search.cpan.org/CPAN/authors/id/J/JP/JPEACOCK/version-0.91.tar.gz ... OK
Configuring version-0.91 ... OK
Building and testing version-0.91 ... OK
Successfully installed version-0.91
--> Working on Exporter
Fetching http://search.cpan.org/CPAN/authors/id/F/FE/FERREIRA/Exporter-5.63.tar.gz ... OK
Configuring Exporter-5.63 ... OK
Building and testing Exporter-5.63 ... OK
Successfully installed Exporter-5.63 (upgraded from 5.58)
--> Working on PPIx::Regexp
Fetching http://search.cpan.org/CPAN/authors/id/W/WY/WYANT/PPIx-Regexp-0.020.tar.gz ... OK
Configuring PPIx-Regexp-0.020 ... OK
==> Found dependencies: PPI::Document, List::MoreUtils
Building and testing PPIx-Regexp-0.020 ... FAIL
! Installing PPIx::Regexp failed. See /root/.cpanm/build.log for details.
--> Working on Config::Tiny
Fetching http://search.cpan.org/CPAN/authors/id/A/AD/ADAMK/Config-Tiny-2.14.tar.gz ... OK
Configuring Config-Tiny-2.14 ... OK
Building and testing Config-Tiny-2.14 ... OK
Successfully installed Config-Tiny-2.14
--> Working on Email::Address
Fetching http://search.cpan.org/CPAN/authors/id/R/RJ/RJBS/Email-Address-1.892.tar.gz ... OK
Configuring Email-Address-1.892 ... OK
Building and testing Email-Address-1.892 ... OK
Successfully installed Email-Address-1.892
! Bailing out the installation for Perl-Critic-1.116. Retry with --prompt or --force.
23 distributions installed
[root]#

更新2

有一个Perl到C编译器,因此可能必须检查所有执行路径以生成C代码,或者至少如果没有,错误的C代码可以通过C编译器传递,这将解决问题:

3 个答案:

答案 0 :(得分:4)

Perl::Criticperlcritic.com)将为您完成部分工作。该模块对源代码进行静态分析。它基于“Perl最佳实践”一书,会发现一些错误,并会参考书的页码给你很多关于风格的建议。

答案 1 :(得分:2)

我认为,如果你第一次加强设计,你将获得最大的效果。

您可以尽可能以防御性方式编码,例如:

  • 尽可能静态编码,以便与Perl :: Critic和编译器等工具配合使用。这样可以在编译时检测错误。

  • 书籍“Perl Best Practices”和“Perl Hacks”(均来自O'Reilly)有一些技巧可以将代码部分从运行时检查转移到编译时检查。

如果你无法避免动态生成的代码,那么就没有替代好的测试套件了。我更喜欢一些手动编写的测试,并结合Test::LectroTest自动生成的测试。 Devel::Cover将帮助您了解测试覆盖率。

我不知道你要求的任何工具,但这种工具的运行时很容易变得不易察觉。所有可能的代码路径的组合通常太多而不实用。

答案 2 :(得分:1)

  • 如果你还没有使用它(我假设你是,但只是确定),use strict; use warnings;会为你找到很多错误,尤其是那些可能由粗心引入的错误编辑,虽然它没有进行任何代码路径分析。

  • 对于代码路径部分,我不知道会自动为所有执行路径生成测试的任何内容,但Devel::Cover将提供代码覆盖率指标,以帮助确定您是否已编写测试你自己缺少任何执行路径。