Keywords: Perl | CPAN Modules | Module Detection | ExtUtils::Installed | File::Find
Abstract: This article provides an in-depth exploration of various methods for detecting installed CPAN modules in Perl environments, focusing on standard solutions using ExtUtils::Installed and File::Find modules. It also analyzes alternative approaches including perldoc perllocal and cpan command-line tools, offering detailed code examples and systematic comparisons to serve as a complete technical guide for Perl developers.
Core Methods for Perl Module Detection
In Perl development environments, understanding the CPAN modules installed on a system is a crucial aspect of daily development work. The official Perl documentation provides standard solutions that can be quickly accessed using the perldoc -q installed command. This command directly references the "How do I find which modules are installed on my system?" section in perlfaq3, which details two primary approaches.
Using the ExtUtils::Installed Module
ExtUtils::Installed is a specialized tool within the Perl standard library for managing installed modules. This module retrieves installation information by reading .packlist files and provides a comprehensive programming interface. Below is a basic usage example:
use ExtUtils::Installed;
my $installed = ExtUtils::Installed->new();
my @modules = $installed->modules();
foreach my $module (@modules) {
my $version = $installed->version($module) || "unknown";
print "$module: $version\n";
}This method accurately identifies modules installed through standard CPAN procedures but may fail to detect modules manually copied into @INC paths.
File System Scanning Approach
As a complementary solution to ExtUtils::Installed, the File::Find module can be used to directly scan Perl's library paths. This approach is more comprehensive, capable of discovering all module files present in @INC:
use File::Find;
my %modules;
find(sub {
return unless /\.pm$/;
my $module = $File::Find::name;
$module =~ s|^.*/lib/||;
$module =~ s|/|::|g;
$module =~ s/\.pm$//;
$modules{$module} = 1;
}, @INC);
print "$_\n" for sort keys %modules;It is important to note that this method lists all modules corresponding to .pm files, including multiple related modules from the same distribution.
Alternative Command-Line Tools
In addition to programming solutions, Perl offers various command-line tools. Using perldoc perllocal allows viewing records of modules installed via CPAN:
perldoc perllocalThe CPAN shell also provides relevant functionality; the cpan -a command can generate an autobundle list of installed modules:
cpan -aThis command not only outputs the module list but also writes the results to a file for future use.
Extended Functionality with Third-Party Tools
Community-developed tools like App::Module::Lister offer more user-friendly interfaces. Designed for CGI environments, this tool requires no additional dependencies and can output modules along with their version information:
use App::Module::Lister;
my $lister = App::Module::Lister->new;
$lister->list_modules;Method Comparison and Selection Recommendations
Each method has its advantages and disadvantages: ExtUtils::Installed is suitable for programming environments but relies on .packlist files; file scanning is the most comprehensive but may include redundant information; command-line tools are convenient and quick but relatively limited in functionality. In practical applications, it is advisable to choose the appropriate method based on specific needs, and for critical systems, consider combining multiple approaches to ensure completeness.
Considerations and Best Practices
It is important to note that the perllocal.pod file only records modules installed via CPAN; manually installed modules are not recorded. In team development environments, if this file is under version control, it may become corrupted due to improper conflict resolution. Therefore, for production environments, it is recommended to establish standardized module management processes and regularly use multiple methods for cross-verification of installation status.