Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a module that searches for superfluous code?

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' );
like image 346
sid_com Avatar asked Oct 09 '22 09:10

sid_com


1 Answers

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.

like image 98
Dan Avatar answered Oct 13 '22 11:10

Dan