Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Finding unused functions and vars?

by Jeppe (Monk)
on Oct 03, 2003 at 13:49 UTC ( [id://296238]=perlquestion: print w/replies, xml ) Need Help??

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

Wise Ones,

How would you go about finding unused functions and vars in your code? The number of lines goes well into the thousands. Some code was made in more foolish times, and some was made before my time. I wish to locate functions that are never called, and variables that are never used.

Luckily, we use strict.

Do you have any tips or experiences to share?

Replies are listed 'Best First'.
Re: Finding unused functions and vars?
by liz (Monsignor) on Oct 03, 2003 at 14:00 UTC
    Maybe Devel::Cover is what you're looking for? I've never used it myself, but it is on my list of things to do for my own modules. If only to find out whether my test-suites are actually testing every aspect of my modules.

    Liz

Re: Finding unused functions and vars?
by Juerd (Abbot) on Oct 03, 2003 at 14:06 UTC

    I wish to locate (...) variables that are never used.

    Let Perl do this for you. Whenever possible, don't declare variables before you use them.

    Bad:

    my ($foo, $bar, $baz, $quux, $blah);
    or
    my $foo; my $bar; my $baz; my $quux; my $blah;

    Good:

    my $foo = /aoeu/ ? 15 : 16; my $bar = Bar->new(); bless \my($baz), $class; open my $quux, '<', $file or die $!; my $bytes_read = read $quux, my($blah), $length;

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: Finding unused functions and vars?
by BUU (Prior) on Oct 03, 2003 at 17:54 UTC
    heh. Heres an idea. First run a perl script to generate a Set containing all of the variables declared in your script. (<code>@set = m/($@%\w_+)/g<code> maybe..). Then delete all of the variables in the script and run it again under use strict. Create another Set containing all the variables strict complains about, then do a simple set intersection!

    (Ok, this probably wouldn't work. But it's a nice idea!)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://296238]
Approved by barrd
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found