koknat has asked for the wisdom of the Perl Monks concerning the following question:
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Cleaning up unused subroutines
by dragonchild (Archbishop) on Oct 26, 2007 at 02:23 UTC | |
My criteria for good software: | [reply] |
Re: Cleaning up unused subroutines
by toolic (Bishop) on Oct 26, 2007 at 03:15 UTC | |
| [reply] |
by koknat (Sexton) on Oct 26, 2007 at 17:31 UTC | |
I hadn't even considered anyonymous subroutines, or objects, since I've been just calling simple normal subroutines. In my simple case, I think a script could be written to: 1) Find all subroutines by doing a grep for ^\s*sub\s+\S+ 2) Find all subroutines called in the main program. 3) Recursively follow every subroutine used, and look for subroutines within subroutines. 4) At the end, you have a list of every subroutine, and a list of every subroutine used The hardest part may be deciding what a simple subroutine call looks like: &mysub &mysub(args) mysub mysub(args) | [reply] |
by ph0enix (Friar) on Oct 29, 2007 at 15:37 UTC | |
| [reply] |
Re: Cleaning up unused subroutines
by BrowserUk (Patriarch) on Oct 26, 2007 at 02:54 UTC | |
Run junk.pl and correct each Undefined subroutine &..... called at ... by removing the XXX_ from the subroutine declarations. Once the script runs again, any sub definitions still carrying the XXX_ prefix are redundant and can safely be removed from yourscript.pl Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by aquarium (Curate) on Oct 26, 2007 at 03:07 UTC | |
the hardest line to type correctly is: stty erase ^H
| [reply] |
by BrowserUk (Patriarch) on Oct 26, 2007 at 03:45 UTC | |
Hm. I'm not for one moment going to suggest that you are wrong. If the code uses prototypes on one or more of the subs, then the regex would need modification. Likewise, as I coded it, it would ignore anonymous subs. But, for the sake of completeness, could you describe what other situations might not be covered? Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
by Somni (Friar) on Oct 26, 2007 at 04:29 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2007 at 04:38 UTC | |
| |
Re: Cleaning up unused subroutines
by tuxz0r (Pilgrim) on Oct 26, 2007 at 01:58 UTC | |
As the previous comment noted, you'll have to suit the solution to your particular code. And, removing calls to subroutines can sometimes create artifacts, especially if those subroutines returned values or took paramaters, so - be careful.
--- | [reply] |
Re: Cleaning up unused subroutines
by roboticus (Chancellor) on Oct 26, 2007 at 03:57 UTC | |
Then after a month or so, I rename all the functions that still have the print in them. (I normally prefix them with a 'z' so they sort to the end of the list. Easy to find 'em if I want 'em, and easy to ignore them when I don't want to see 'em.) ...roboticus | [reply] [d/l] |
Re: Cleaning up unused subroutines
by aquarium (Curate) on Oct 26, 2007 at 01:41 UTC | |
use strict and similar may help somewhat...but no guarantees. as a (very) rough guide, count the occurences that grep found for the function name in the code
the hardest line to type correctly is: stty erase ^H
| [reply] |
Re: Cleaning up unused subroutines
by dmorgo (Pilgrim) on Oct 27, 2007 at 09:29 UTC | |
"subvirgin" subvirgin finds virgin subroutines. Well, more accurately it lets you find virgin subroutines by a process of elimination, because it logs each subroutine as its used. You still have to do the work of figuring out which subroutines were not used. OK, so there's some room for improvement there, I admit, but still, it's pretty nifty if I do say so myself. One more caveat: it won't do a great job unless you can, through your own ingenuity and luck, get the code you are examining to run through most of its possible execution paths.All it does is insert some code at the top of each subroutine, using an appropriately-named subroutine, "insert_instrument()" ... cough. This code calls a small SVGN.pm module that logs the name of each subroutine as it runs. In case it's not obvious, the main value of this tool would be in situations where you are faced with a large, unfamiliar Perl code base and have been told that some of the code is old, orphaned code that is no longer used, and you want to determine which code is still used and which is not (though for the negatives, it won't be with 100% reliability, unfortunately). You can add your own code to the SVGN module shown at the bottom, so it could do fancier stuff, like, for example, printing the subroutine's caller and arguments. But for now I'll leave that as an exercise for the reader. Here's the code. Oh, and it doesn't do anonymous or nested subroutines at the moment... sorry about that. Should be easy to fix. **** WARNING: This has not been extensively tested, and more to the point, IT WILL DO A DESTRUCTIVE UPDATE OF ALL PERL FILES IT FINDS IN THE CURRENT DIRECTORY so use it carefully and work on a copy of your code. *** And here's the skeleton for SVGN.pm
| [reply] [d/l] [select] |
Re: Cleaning up unused subroutines
by burningdog (Scribe) on Oct 27, 2007 at 14:20 UTC | |
| [reply] |
Re: Cleaning up unused subroutines
by neszt76 (Novice) on Sep 19, 2018 at 08:02 UTC | |
| [reply] [d/l] |
by hippo (Archbishop) on Sep 19, 2018 at 08:58 UTC | |
Hello neszt76 and welcome to PerlMonks. We use Perl here.
This uses precisely* the same logic as your shell loop above (and therefore has the same algorithmic flaws). Nonetheless, advantages of the Perl approach include:
Enjoy. *Not precisely: there's one trivial fix to avoid lines which start with eg. "submarine". | [reply] [d/l] |
Re: Cleaning up unused subroutines
by kelpless (Initiate) on Mar 11, 2011 at 00:04 UTC | |
I use cloc (cloc.sf.net) in a bash script I call 'apparently_unused'
This is only 'apparently' unused subroutines because of calls that can be made in the way others have pointed out that this simple grep will miss. The cloc program cleans up a lot of false negatives by removing comments that may include subroutine names. Also, this assumes you have 'sub' lines that start at the first column - use perltidy. | [reply] [d/l] |