Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Remove an Installed Module

by casiano (Pilgrim)
on Oct 13, 2008 at 21:25 UTC ( [id://716861]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info Casiano
Description: Remove a installed Perl distribution
use strict;
use ExtUtils::Packlist;
use ExtUtils::Installed;
use List::Util qw(first);

$ARGV[0] or die "Usage: $0 Module::Name\n";

my $mod = $ARGV[0];

die "Does not looks like a module name"
  unless $mod =~ m{\w+(::\w+)*};

my $inst = ExtUtils::Installed->new();

die "Can't find module $mod using .packlist files\n"
  unless first { /$mod/ } ( $inst->modules );
foreach my $item ( sort( $inst->files($mod) ) ) {
    print "removing $item\n";

      unlink $item
    or warn "Can't remove $item try by hand!\n";
}

my $packfile = $inst->packlist($mod)->packlist_file();
print "removing $packfile\n";

  unlink $packfile
or warn "Can't remove $packfile try by hand!\n";

=head1 NAME

pmrm -- remove installed perl distribution module


=head1 SYNOPSIS

    pmrm Some::Module
Replies are listed 'Best First'.
Re: Remove an Installed Module
by bingos (Vicar) on Oct 14, 2008 at 08:48 UTC
Re: Remove an Installed Module
by CountZero (Bishop) on Oct 14, 2008 at 06:22 UTC
    Nice program, but perhaps one caveat is useful: removing a module or distribution may break other modules or distributions which depend on the removed module or distribution.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Remove an Installed Module
by Tux (Canon) on Oct 14, 2008 at 12:06 UTC

    Nice indeed, but next to the dependency problem noted by CountZero, the script also doesn't rmdir the empty folders it leaves behind.


    Enjoy, Have FUN! H.Merijn
Re: Remove an Installed Module
by hackman (Acolyte) on Jan 27, 2011 at 08:58 UTC
    Since this is very useful information. I would like to add that:

    ExtUtils::Packlist

    Has a script that is better then the above. If you want to limit it to a single module you can simply add:
    next if ($module ne $ARGV[0]);
    After the the grep line.

    One Planet, One Internet... We Are All Connected...
      Where what?
        Original Example:
        #!/usr/local/bin/perl -w use strict; use IO::Dir; use ExtUtils::Packlist; use ExtUtils::Installed; sub emptydir($) { my ($dir) = @_; my $dh = IO::Dir->new($dir) || return(0); my @count = $dh->read(); $dh->close(); return(@count == 2 ? 1 : 0); } # Find all the installed packages print("Finding all installed modules...\n"); my $installed = ExtUtils::Installed->new(); foreach my $module (grep(!/^Perl$/, $installed->modules())) { my $version = $installed->version($module) || "???"; print("Found module $module Version $version\n"); print("Do you want to delete $module? [n] "); my $r = <STDIN>; chomp($r); if ($r && $r =~ /^y/i) { # Remove all the files foreach my $file (sort($installed->files($module))) { print("rm $file\n"); unlink($file); } my $pf = $installed->packlist($module)->packlist_file(); print("rm $pf\n"); unlink($pf); foreach my $dir (sort($installed->directory_tree($module))) +{ if (emptydir($dir)) { print("rmdir $dir\n"); rmdir($dir); } } } }
        And you can simply add:
        next if ($module ne $ARGV[0]);
        after the grep line and you will effectively make this script the equivalent of rm for perl.
        I actually have it named rmpmod on all of my machines.
        For example:
        $ rmpmod DBD::Pg
        Removes the DBD::Pg module from the machine.
        One Planet, One Internet...
        We Are All Connected...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-11-10 07:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    chatterbot is...






    Results (37 votes). Check out past polls.