Take the 2-minute tour ×

Is there a module, which can find code not needed?
As an example a script with code not needed to run the script:

#!/usr/bin/env perl
use warnings;
use 5.12.0;
use utf8;
binmode STDOUT, ':utf8';
use DateTime;
use WWW::Mechanize;

sub my_print {
    my ( $string, $tab, $color ) = @_;
    say $string;

sub check {
    my $string = shift;
    return if length $string > 10;
    return $string;

my_print( 'Hello World' );
share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Not categorically. Perl is notoriously difficult to analyze without actually executing, to the point that compiling a Perl program to be run later actually requires including a copy of the perl interpreter! As a result there are very few code analysis tools for Perl. What you can do is use a profiler, but this is a bit overkill (and as I mentioned, requires actually executing the program. I like Devel::NYTProf. This will spit out some HTML files showing how many times eaqch line or sub was executed, as well as how much time was spent there, but this only works for that specific execution of the program. It will allow you to see that WWW::Mechanize is loaded but never called, but it will not be able to tell you if warnings or binmode had any effect on execution.

share|improve this answer
Devel::NYTProf is a good tool for the job, but to expand on the phrase, "...but this only works for that specific execution of the program.": A given run may not follow all logic paths, and consequently, those paths not followed won't show up in the profile. However, that doesn't mean they're unreachable -- just that they weren't reached on a particular run. –  DavidO Mar 14 '12 at 12:15
Thanks for the clarification DavidO, +1 –  Dan Mar 14 '12 at 20:30
add comment

Devel::Cover provides code coverage metrics that may be of some use here.

share|improve this answer
add comment

Your Answer


