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

Find doubly installed modules

by Sixtease (Friar)
on Apr 01, 2007 at 19:25 UTC ( #607723=sourcecode: print w/replies, xml ) Need Help??
Category: miscellaneous
Author/Contact Info Oldrich Kruza <Oldrich.Kruza@sixtease.net> http://www.sixtease.net/
Description: Run this script from the perllib directory, where vendor_perl and site_perl directories reside. (in my case it's /usr/lib/perl5) The script will output the modules (.pm files) located in both subdirs. The branch (site_perl or vendor_perl) will be output for the older module (which you thus might want to remove).
#!/usr/bin/perl

=pod

Run this script from the perllib directory, where vendor_perl and site
+_perl
directories reside. (in my case it's /usr/lib/perl5)
The script will output the modules (.pm files) located in both subdirs
+.

The branch (site_perl or vendor_perl) will be output for the older mod
+ule
(which you thus might want to remove).

Set $PERLVERSION to whatever you have in both site_perl and vendor_per
+l.

=cut

use strict;
use warnings;

my $PERLVERSION = '5.8.8';

# print each element of a list on a separate line
$\ = $, = "\n";

my $s = 'site_perl';
my $v = 'vendor_perl';
my @pms = ();

# takes a list of dirnames that are in both branches
# returns a list of files that are in these dirs
# in both branches (the branch prefix is truncated)
sub common {
    my @dirs = @_;
    my @rv = ();
    for my $dir (@dirs) {
        my @site   = glob("$s/$dir/*");
        my @vendor = glob("$v/$dir/*");
        s#^.*?/## for @site, @vendor;
        push @rv, grep {my $s = $_; grep {$_ eq $s} @vendor} @site;
    }
    return @rv;
}

# initialization
# The array contains files that are in both branches
# of one level of the subtree hierarchy.
my @commons = common $PERLVERSION;

# store the modules common to both branches.
# Processes one level of subtree depth per iteration.
while (@commons) {
    # store the modules - they will be output
    push @pms, grep {m/\.pm$/} @commons;
    # keep only directories for further traversal
    @commons = grep {-d "$s/$_"} @commons;
    # dive one level deeper
    @commons = common @commons;
}

# Print the found modules.
# Try to figure out the version of each
# and print the older one.
# Don't print a branch if neither seems to be older.
for my $pm (@pms) {
    # $spm --   site_perl PM
    # $vpm -- vendor_perl PM
    open my $spm, '<', "$s/$pm" or warn "Couldn't open $s/$pm";
    open my $vpm, '<', "$v/$pm" or warn "Couldn't open $v/$pm";

    # look for the version of each module
    # $sV --   site_perl module's VERSION
    # $vV -- vendor_perl module's VERSION
    no warnings 'uninitialized';
    my ($sV) = grep /\$VERSION\s*=/, <$spm>;
    my ($vV) = grep /\$VERSION\s*=/, <$vpm>;
    $sV =~ /\$VERSION\s*=\D*([\d.]+)/;
    $sV = $1;
    $vV =~ /\$VERSION\s*=\D*([\d.]+)/;
    $vV = $1;

    my $leadstr =
    $sV > $vV ?   "$v/" :
    $sV < $vV ? "  $s/" :
                "     " ;
    print $leadstr . $pm;
}
Replies are listed 'Best First'.
Re: Find doubly installed modules
by grinder (Bishop) on Apr 01, 2007 at 20:46 UTC

    Rather than inventing your own technique for determining versions, use battle-tested code:

    perl -MExtUtils::MakeMaker -le 'print MM->parse_version(shift)' /path/to/module.pm

    That is likely to be much more robust. Otherwise, ++, a very nice hack.

    • another intruder with the mooring in the heart of the Perl

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2021-06-21 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (98 votes). Check out past polls.

    Notices?