Back about two years ago (Date: Wed, 18 Apr 2012 21:44:31 +0000 (UTC);
Message-ID: said:
Looking at the docs, it appears that Devel::Modlist reports on *all*
the modules used in an entire script. Is there some way I can
determine the modules used, directly or transitively, merely by the
current module?
Ben Morrow suggested in said:
perl -d:Modlist -mMy::Module -e1 ?
I've gotten around to trying that. The problem is that apparently I
had a different problem in mind than today's when I wrote
determine the modules used, directly or transitively
With the method above, it's indeed transitive. (Also, if I leave out
something from the INC path, it errors out at that "use" statement and
doesn't check anything after. Our system does have a non-trivial
include list.)
For today's problem, I want only "directly", not "transitively".
My group is adding calls subs in new modules and sometimes forgetting
to add the "use" statements. Each of our modules should "use"
everything it calls. I'd like a somewhat automated way to check
statically for missing "use"s before we check in files. So I do not
want transitive uses, and apparently we're doing a fair number of
transitive "use"s.
(I tried -d:Modlist as above with a library path that has only
Devel::Modlist, but the problem above arises: since I'm leaving out
many things from the INC path, it errors out at the first "use" of one
of our modules and doesn't report anything after. And if I include our
path, as aforesaid I get a lot of transitive uses that I don't want.)
I'm thinking that I should just write a simple script to read a Perl
file looking for
- ^use foo::bar::baz
save it as one of the uses (ignored any suffixed "nocritic", "no",
or whatever)
- foo::bar::baz->
confirm that foo::bar::baz is in the hash of uses
- foo::bar::baz::quux not followed by ->
confirm that foo::bar::baz is in the hash of uses
We very rarely do "require". We rarely do exports. So the above
should work well enough 98% of the time. I'm OK with false positives
in such odd cases, and false negatives (like calling an exported sub
where you forgot to "use" the module directly) should be rare and you
deserve the severe tire damage if you do it.
Comments? Better ideas?