求等效于pip freeze的Perl工具:获取指定目录下Perl模块及版本
pip freeze --user) Got it, let's figure out how to get that list of Perl modules (and their versions) from your custom install directory—just like Python's pip freeze --user does for user-specific packages. Here are a couple of solid methods to make this happen:
Method 1: Custom Perl Script (No Extra Dependencies)
If you don't want to install any extra tools, a quick Perl script can traverse your target directory, parse module names, and pull their versions. This works with any standard Perl setup.
Save this as perl_freeze.pl:
#!/usr/bin/perl use strict; use warnings; use File::Find; # Replace this with your actual module directory my $target_dir = '/soft/foo/1.0.0/lib/perl5'; # Walk through all .pm files in the directory find(sub { # Skip non-.pm files return unless $_ =~ /\.pm$/; # Convert file path to Perl module name (e.g., Foo/Bar.pm → Foo::Bar) my $full_path = $File::Find::name; my $module_path = $full_path; $module_path =~ s/^\Q$target_dir\E//; # Remove base directory $module_path =~ s/\.pm$//; # Strip .pm extension $module_path =~ s/\//::/g; # Replace slashes with :: $module_path =~ s/^:://; # Clean up leading :: # Load the module and fetch its version eval { require $full_path; my $version = ${"${module_path}::VERSION"}; # Handle modules without a defined VERSION print "$module_path==" . (defined $version ? $version : '0') . "\n"; }; # Warn if a module fails to load (e.g., missing dependencies) warn "Skipping $module_path: $@" if $@; }, $target_dir);
Then run it with:
chmod +x perl_freeze.pl ./perl_freeze.pl
This will output lines like Data::Dumper==2.183—exactly the format you need for reproducibility.
Method 2: Use cpanm (If You Have It Installed)
If you use cpanminus (a popular Perl package installer), you can leverage its built-in --list flag to get a clean list. First, point Perl to your custom directory, then run:
PERL5LIB=/soft/foo/1.0.0/lib/perl5 cpanm --list
This will output lines like Data::Dumper /soft/foo/1.0.0/lib/perl5/Data/Dumper.pm. If you want the Module==Version format, pipe it to a quick awk command to extract the version:
PERL5LIB=/soft/foo/1.0.0/lib/perl5 cpanm --list | awk '{ module=$1; path=$2; # Run a tiny Perl command to get the version from the module file cmd="perl -e '\''require qq("$path"); print \$" module "::VERSION || 0'\''"; cmd | getline version; close(cmd); print module "==" version; }'
Notes to Keep In Mind
- Some modules don’t define a
$VERSIONvariable—both methods will default to0for these cases. - If a module fails to load (e.g., it depends on another module not in your custom directory), the script will warn you but keep processing other modules.
- For binary Perl modules (in the
archsubdirectory), the script will still handle their accompanying.pmfiles correctly.
内容的提问来源于stack exchange,提问作者Eric Deveaud

