Removing Perl modules installed from cpan

The cpan utility will help you install any Perl module. The thing is that cpan downloads the source archives, compiles them and installs them. The problem shows up when you need to remove such a module.

The following script will help you get rid of the modules you don’t need:

# uninstall_perl_module.pl from PerlTricks.com

use 5.14.2;
use ExtUtils::Installed;
use ExtUtils::Packlist;

# Exit unless a module name was passed
die ("Error: no Module::Name passed as an argument. E.G.\n\t perl $0 Module::Name\n") unless $#ARGV == 0;

my $module = shift @ARGV;

my $installed_modules = ExtUtils::Installed->new;

# iterate through and try to delete every file associated with the module
foreach my $file ($installed_modules->files($module)) {
    print "removing $file\n";
    unlink $file or warn "could not remove $file: $!\n";
}

# delete the module packfile
my $packfile = $installed_modules->packlist($module)->packlist_file;
print "removing $packfile\n";
unlink $packfile or warn "could not remove $packfile: $!\n";

# delete the module directories if they are empty
foreach my $dir (sort($installed_modules->directory_tree($module))) {
    print("removing $dir\n");
    rmdir $dir or warn "could not remove $dir: $!\n";
}

Run it like this:

perl uninstall_perl_module.pl Acme::Dot

The alternative is to clean up by hand:

cd ~/.cpan/build  
cd module-name-xyz  
make uninstall

Follow the instructions.

You can also try your luck with App::cpanminus. It is installed with cpan

and used like this:

cpanm -uninstall Acme::Dot

perltricks.com
coderwall.com

Categories:

Updated: