TooMuchCode::ProhibitUnusedImport -- Find unused imports
An "Unused Import" is usually a subroutine name imported by a use statement. For example, the word Dumper in the following statement:
use
Dumper
use Foo qw( baz );
The word baz can be removed if it is not mentioned in the rest of this program.
baz
Conventionally, this policy looks only for the use statement with a qw() operator at the end. This syntax is easier to deal with. It also works with the usage of Importer module -- as long as a qw() is there at the end:
qw()
Importer
use Importer 'Foo' => qw( baz );
This may be adjusted to be a bit smarter, but it is a clear convention in the beginning.
Modules which will be ignored, generally because the args of import do not mean the symbols to be imported.
[TooMuchCode::ProhibitUnusedImport] ignored_modules = Git::Sub Regexp::Common
When importing types from a Moose type library, you may run into the following situation:
use My::Type::Library::Numeric qw( PositiveInt ); my $foo = 'bar'; my $ok = is_PositiveInt($foo);
In this case, My::Type::Library::Numeric exports is_PositiveInt as well as PositiveInt. Even though PositiveInt has not specifically been called by the code, the import has clearly been used. In order to allow for this case, you can specify class names of Moose-like type libraries which you intend to import from.
My::Type::Library::Numeric
is_PositiveInt
PositiveInt
A similar case exists for coercions:
use My::Type::Library::String qw( LowerCaseStr ); my $foo = 'Bar'; my $lower = to_LowerCaseStr($foo);
In the above case, to_LowerCaseStr has not specifically been called by the code, but the import has clearly been used.
to_LowerCaseStr
The imports of is_* and to_* from the following modules be handled by default:
is_*
to_*
* MooseX::Types::Moose * MooseX::Types::Common::Numeric * MooseX::Types::Common::String
You can configure this behaviour by adding more modules to the list:
[TooMuchCode::ProhibitUnusedImport] moose_type_modules = My::Type::Library::Numeric My::Type::Library::String
To install Perl::Critic::TooMuchCode, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl::Critic::TooMuchCode
CPAN shell
perl -MCPAN -e shell install Perl::Critic::TooMuchCode
For more information on module installation, please visit the detailed CPAN module installation guide.