Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
The stupid question is the question not asked
 
PerlMonks  

Re: Comparing what Modules are Installed on Two DIfferent Versions of Perl?

by thunders (Priest)
on Nov 16, 2009 at 20:07 UTC ( #807553=note: print w/ replies, xml ) Need Help??


in reply to Comparing what Modules are Installed on Two DIfferent Versions of Perl?

Here's a script I called "module_list.pl" I wrote it up a while back to do a quick inventory on different systems I maintained. It will give you a sorted list of all modules installed, if you give it a "-v" option it will also show the path to that modules location. Just call it with each version of perl installed and it should do the right thing.

#!/usr/bin/perl use strict; use warnings; =pod =head1 module_list.pl prints a list of all available perl modules and their location on disk =cut package ModuleLookup; use File::Find; use File::Spec; use Cwd qw(cwd realpath); =head1 ModuleLookup Module used by module_list.pl to scan the system for installed perl mo +dules =cut #cached lookups my %_FILE_TO_CLASSNAME_CACHE; my %_PATH_CACHE; #absolute path of all modules in the system my @ABS_INC = grep { $_ ne cwd() } #remove working directory from p +ath map { _cached_rel2abs($_) } @INC; =head1 $lookup = ModuleLookup->new(); Returns a ModuleLookup object that can be used to find installed perl +modules =cut sub new { my $class = shift; my $self = {}; return bless $self, $class; } =head1 $modules = $lookup->perl_modules_on_system() Returns a hash reference containing all perl modules available in the +current environment keyed by class name =cut sub perl_modules_on_system { my $self = shift; return $self->perl_modules_in_paths(@ABS_INC); } =head1 $modules = $lookup->perl_modules_in_paths(@paths) Given a list of directory paths, returns a hash reference containing p +erl modules under those directories keyed by class name =cut sub perl_modules_in_paths { my ( $self, @paths ) = @_; my %perl_modules; my @valid_paths; for my $path (@paths) { if ( -l $path){ $path = realpath($path); } if ( !-d $path ) { warn("invalid path $path"); next; } push @valid_paths, $path; } find( sub { if ( $File::Find::name =~ /\.pm$/ ) { my $path = $File::Find::name; my $class = $self->classname_from_path($path); $perl_modules{$class} = $path; } }, @valid_paths ); return \%perl_modules; } =head1 $classname = $lookup->classname_from_path($path) Given a path to a perl module, returns the class name expected by B<us +e> =cut sub classname_from_path { my ( $self, $path ) = @_; #avoid looking up the same path multiple time (File::Spec::real2abs ca +n be expensive) my $full_path = _cached_rel2abs($path); if ( exists $_FILE_TO_CLASSNAME_CACHE{$full_path} ) { return $_FILE_TO_CLASSNAME_CACHE{$full_path}; } my $classpath = $self->classpath_from_fullpath($full_path); #convert path to module name ( my $classname = $classpath ) =~ s/\.pm//; $classname =~ s{/}{::}g; $_FILE_TO_CLASSNAME_CACHE{$full_path} = $classname; return $classname; } =head1 $classpath = $lookup->classpath_from_fullpath($path) Given a path to a perl module, returns a relative path minus the perl +include directory =cut sub classpath_from_fullpath { my ( $self, $full_path ) = @_; #Remove the perl include path from the directory. my $max_substr = 0; my $parent_dir; for my $inc_dir (@ABS_INC) { my $dir_index = index( $full_path, $inc_dir ); # In the case that one @INC directory is contained within anot +her # (i.e. /usr/lib/perl /usr/lib/perl/5.10.0) # we remove the longest path that matches if ( $dir_index >= 0 ) { if ( length($inc_dir) > $max_substr ) { $max_substr = length($inc_dir); $parent_dir = $inc_dir; } } } my $classpath = $full_path; if ($parent_dir) { $classpath =~ s{^$parent_dir/}{}; } return $classpath; } sub _cached_rel2abs { my $path = shift; if ( !exists $_PATH_CACHE{$path} ) { my $abs_path = File::Spec->rel2abs($path); $_PATH_CACHE{$path} = -l $abs_path ? realpath($abs_path) : $ab +s_path; } return $_PATH_CACHE{$path}; } 1; package main; use Getopt::Long; my %opts; GetOptions( \%opts, 'help','verbose' ); if ( $opts{help} ) { print <<"EOD"; Usage: $0 - prints a list of all available perl modules and their loca +tion on disk For more information: perldoc $0 EOD exit; } my $module_lookup = ModuleLookup->new(); my $modules = $module_lookup->perl_modules_on_system(); #send a simple report to STDOUT for my $module ( sort { lc($a) cmp lc($b) } keys %$modules ) { if($opts{verbose}){ printf( "%35s : %-40s\n", $module, $modules->{$module} ); } else { printf( "%s\n", $module); } } print scalar( keys %$modules ) . " perl modules installed on this syst +em\n"; 1;


Comment on Re: Comparing what Modules are Installed on Two DIfferent Versions of Perl?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://807553]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (15)
As of 2014-04-17 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (453 votes), past polls