therobyouknow
therobyouknow

Reputation: 6800

How to check for all errors in a Perl-CGI application (e.g. check with a Perl compiler or lint-like tool, free opensource?)

I have a mission critical Perl-CGI server-side application that I need to extend or enhance from time to time.

Is there a tool that will check all the execution paths of the application, i.e. all Perl files, all functions, all if-else, switch-case etc possibilities.

Being an interpreted language, errors introduced during modifying code don't always become apparent. One can be misled if one thinks that everything is OK, if after doing the changes, the Perl program runs OK after doing some tests to check.

A comprehensive test suite might be a solution, but this is always going to be custom to the program and takes additional time to implement, when time is scarce.

Rather, I think an effective tool would be generic to "blanket" exercise/parse/whatever every execution path e.g. checking for undeclared variables, change to function/subroutine names e.g. due to refactoring etc.

I have ActiveState Komodo IDE which does a good job at detecting errors but being a commercial tool, I won't always be able to use it in all the environments I work in. So I would prefer something free and open source.

Update

Thanks @Nylon Smile for Perl::Critic - the answer below - I didn't have it as standard in my Perl setup so had to use cpan to try to install it, of course :)

I'm seeing lots of Successes in the verbose install progress (see below), but a few failures too :( and also a "Bailing out the installation for Perl-Critic-1.116" which doesn't look good :(

This is not uncommon whenever I try to install or upgrade Perl modules. I have CentOS 5.

It leaves me with the impression that maintaining, configuring, upgrading Perl is a rather messy affair, flakey, fiddly, probably with "but-you-didn't" and "if-you" regarding what you have to do to make it work :)

[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]#

Update 2

There is a Perl to C compiler, so presumably this would have to examine all execution paths to produce C code, or at least if not, the erroneous C code could be passed through a C compiler that would pick up on the problems:

Upvotes: 1

Views: 462

Answers (3)

hexcoder
hexcoder

Reputation: 1220

I think, you will get the most effect, if you first enhance the design.

You could code as defensively as possible, for example:

  • Code as statically as possible in order to cooperate with tools like Perl::Critic and the compiler. This enables the detection of errors at compile time.

  • The books 'Perl Best Practices' and 'Perl Hacks' (both from O'Reilly) have some tricks for moving code parts from runtime checking to compiletime checking.

If you cannot avoid dynamically generated code, there is no substitute for a good test suite. I prefer some manually written tests combined with automatically generated tests from Test::LectroTest. Devel::Cover will help you to get a feeling of your test coverage.

I don't know of any tool like the one you ask for, but the runtime of such a tool could easily be unacceptible. The combinatorics of all possible code paths is often too much to be practical.

Upvotes: 2

Dave Sherohman
Dave Sherohman

Reputation: 46187

  • If you're not already using it (I assume you are, but just making sure), use strict; use warnings; will find a lot of errors for you, especially of the sorts likely to be introduced by careless edits, although it doesn't do any code path analysis.

  • For the code path portion, I'm not aware of anything that will automatically generate tests for all execution paths, but Devel::Cover will provide code coverage metrics to help determine whether the tests you've written yourself are missing any execution paths.

Upvotes: 1

Nylon Smile
Nylon Smile

Reputation: 9436

Perl::Critic (perlcritic.com) will do some of the work for you. The module does a static analysis of your source code. It's based on the book "Perl Best Practices" and will find some bugs and will give you many advices on the style with reference to the page number of the book.

Upvotes: 4

Related Questions