Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Check if all used modules are use'ed

by Sewi (Friar)
on Oct 27, 2011 at 09:32 UTC ( [id://934093]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

Is there any (script|module|etc.) which reads a Perl script and complains about every module being used in the source but not imported using "use Module;"?

Sample:

use My::Foo; # Foo is using My::Bar $x = My::Bar->new;

Perl will run this source without complaining - until every usage of My::Bar and the "use My::Bar" are removed from Foo.pm.

Thanks

Replies are listed 'Best First'.
Re: Check if all used modules are use'ed
by Corion (Patriarch) on Oct 27, 2011 at 09:49 UTC

    I would assume that Module::Scandeps can do that. Alternatively, if you are worried about implicit loading of modules, I would just grep for everything that looks like a class name (/\b(\w+(?:'|::)(?:\w+(?:'|::)?)*/), and see where else that shows up. Note that use $module isn't sufficient, as there are many other "loader-style" modules, like if.

        scandeps isn't lint :)
        $ perl -MO=Xref -e " print $Foo::Bar::x " | perl -ne " $f++ if /Subro +utine..main/; print if $f " -e syntax OK Subroutine (main) Package Foo::Bar $x 1
Re: Check if all used modules are use'ed
by JavaFan (Canon) on Oct 27, 2011 at 09:48 UTC
    No, there is not such a script.

    Take the following code, which module is used, but not imported?

    #!/usr/bin/perl use strict; use warnings; my $class1 = $ARGV[rand @ARGV]; my $class2 = $ARGV[rand @ARGV]; $class1 =~ s{::}{/}g; require "$class1.pm"; $class2->method(); __END__
    Even if you know the arguments, you still don't know which module is going to be used, and whether it's used without being required.

      Finding all special cases would require a package/namespace list from a running task.

      Any solution finding the basic things would be ok as there are very few (if any) code pieces with dynamic dependencies in this project.

Re: Check if all used modules are use'ed
by Tux (Canon) on Oct 27, 2011 at 11:16 UTC

    Not the way you describe the wish, but I've had enormous help of Devel::TraceUse which will show - when the program finishes - a list of modules actually loaded with their versions.


    Enjoy, Have FUN! H.Merijn
Re: Check if all used modules are use'ed
by CountZero (Bishop) on Oct 27, 2011 at 22:21 UTC
    Just run the program through its paces and see if you get any errors. Of course your test suite will discover such errors quickly.

    I am more worried about modules "use"d but never used, because that is a waste of memory, processing power and time.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      All web scripte are running in a persistant interpreter (mod_perl/FastCGI), every module is used sooner or later.

      Problems start when a script is called which doesn't use a used module and no other script has pre-loaded (use'd) the module before.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found