Jaap has asked for the wisdom of the Perl Monks concerning the following question:

Holy Monks,

Are there any tools for checking perl code beyond what the compiler checks?
I am thinking of checking caveats like unused code, unused variables, redundancy, not checking return values of subs and system calls, etc.

If such a tool does not exist, what would you like to see in it if 'the community' would make one?

Replies are listed 'Best First'.
Re: code checker
by bart (Canon) on Jan 23, 2003 at 11:10 UTC
    Check out B::Xref. It comes with the standard distribution. You invoke it by running this at the command line:
    perl -MO=Xref yourscript.pl
    for a human readable form, outlined; or
    perl -MO=Xref,-r yourscript.pl
    for the raw table output.

    It's a bit coarse around the edges, but it'll do most of what you want, I think.

    Update: In addition, you might be interested in the profiling modules in the Devel::* hierarchy. Check out the series of articles by brian d foy on DDJ's website, like this one, on Devel::Coverage.

      This is a very usefull module (and new to me). Although not exactly what i had in mind, it is pretty cool.
Re: code checker
by Coruscate (Sexton) on Jan 23, 2003 at 10:47 UTC

    • Unused code: By this, do you mean subroutines that never get called? Blocks such as if (0) {}?
    • Unused variables: the -w switch does this one for you.
    • Redundancy: I'm not sure how hard it would be to locate code redundancy, but I'm fairly sure it be an extremely tough one to accomplish. I'm not saying it's a bad idea, it would be amazing for code optimization, but I'm quite skeptical as to how one would go about this.
    • Not checking return values: yes, this one could be done, but only to a *very* limitted extent! Not all return values need to "be checked" and have a die() if it's not successful. More often than not, subroutines return a value to be passed to a scalar or onto another subroutine, so it's an actual data value being returned, not a 'success or fail' one.

    Who knows, maybe it would be easier to code than I think (except for that redundancy one, that's just impossible!). I think many people dream about perfect code that never needs to be improved.

    Are there any tools for checking perl code beyond what the compiler checks?

    Haven't you ever heard of perlmonks? Post your code on that site and see if anyone will give a helping hand in suggesting ways in which you could improve your code. Not many people will rewrite it for you, but many more will offer their wisdom. :)

    Update (I seem to like these): A couple of people have wanted to see a new section created on the site for 'pay to get code and answers'. If anything, let's create a 'Improve This' section where users can post their code and ask for direction on how to improve the way in which things are handled. It could be said that these could be posted to SoPW, but I'm sure several people would become frustrated if SoPW were to be suddenly filled with requests of 'make my code better for me!'. I'm sure that if it weren't always the same few people offering up advice on coding things 'better' and everybody tried (whether successfully or not) to help out. I seem to think that everyone would be happy to have better code :)

      I second the concept about asking IMC (Improve my code) section. The original code should be a working one in this case. It can certainly promote faster learning. We all can improve on our code with better understanding initally conciously and then subconciously, with this section.

      I like the concept very much as we all want to accomplish certain thing with the code. Improvement Suggestion would enhance the performance of the code, and style. We have our best way of doing the things in perl. The perl motto of TIMTOWDI would serve us better in this as we can find multiple ways to do the same thing and improve our best way doing the coding.


Re: code checker
by Tanalis (Curate) on Jan 23, 2003 at 10:43 UTC
    I'd suggest you take a look at perl -w and/or use warnings; - they'll provide a lot of useful information for some (although not all) of what you need, for example warning about variables that are only used once, and (known) calls that can return a zero value that haven't been checked for.

    Perldoc has some useful information about using warnings here.

    Above and beyond that, I'm not aware of any tool that does what you ask - maybe others can point you in a better direction there.

    Hope that helps ..
    -- Foxcub

Re: code checker
by artist (Parson) on Jan 23, 2003 at 18:43 UTC
    There is an interesting discussion going on about redundancy checking at Slashot

    Two Stanford researchers (Dawson Engler and Yichen Xie) have written a paper (pdf) showing that seemingly harmless redundant code is frequently a sign of not so harmless errors.


      Yes this is the article that made me wonder whether there is a lint for perl.
Re: code checker
by Tommy (Chaplain) on Jan 24, 2003 at 04:50 UTC

    take a look at the Development Support family of modules at the CPAN.. There are modules that will tell you what parts of your code are used and how often, report variables and their sizes, profile your code line by line - and lots more; too much to write about. Start with the standard Devel::DProf module to profile your code. Take a look at the standard distribution module "Benchmark" for benchmarking.

    Read the perldebug section of the Perl docs about the Perl debugger, and perldebtut for a tutorial on using it. It offers interactive, step-through debugging and other cool stuff.

    The Komodo IDE at activestate.com provides a GUI front-end to the Perl debugger.

    Take a look at perltidy for code beautification and simple syntax verification. It's written in Perl too.

    Tommy Butler, a.k.a. TOMMY
Re: code checker
by Wysardry (Pilgrim) on Jan 24, 2003 at 03:53 UTC

    "If such a tool does not exist, what would you like to see in it if 'the community' would make one?"

    I'd like to see a TMTOWTDI tool/module that not only tells you how many other ways there are to do it, but what they are and rates them out of 10 for style, efficiency, portability and readability. :D

    Or did you want only practical features? ;)

    "Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to one instruction which doesn't work." -- (Author Unknown)

Re: code checker
by Petras (Friar) on Jan 24, 2003 at 01:48 UTC
    If you post the code at most Perl groups, and malignantly claim that it throws a 500 error, you'll have hundreds of good minds above and beyond the compiler checking over your code for you.

    At first you'll get a couple hundred messages asking about permissions settings.

    I'd suggest not trying this. ;)
Re: code checker
by jacques (Priest) on Jan 24, 2003 at 14:09 UTC
    You might be interested in the GNU Data Display Debugger (DDD), which is a graphical front-end for the Perl debugger (and many other debuggers). Consider it a free alternative to the ActiveState debugger tool.