Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^4: Algorithm For Uninstalling Dependencies

by Limbic~Region (Chancellor)
on Nov 20, 2009 at 23:26 UTC ( #808548=note: print w/replies, xml ) Need Help??

in reply to Re^3: Algorithm For Uninstalling Dependencies
in thread Algorithm For Uninstalling Dependencies

After an hour an a half, I convinced my co-worker he was full of contradictory statements and what he said he needed wasn't at all the correct definition of the problem. After figuring out what he really wanted to do, it turned out to be a simple matter of constructing a "depends on me" tree and then using the topological sort.
#!/usr/bin/perl use strict; use warnings; my %all_pkg; my %want_rm; my %must_keep = map {$_ => 1} grep {! $want_rm{$_}} keys %all_pkg; my %tree; for my $pkg (keys %all_pkg) { push @{$tree{$_}}, $pkg for dependencies($pkg); } PACKAGE: for my $pkg (keys %want_rm) { my @rm_order = how_to_uninstall($pkg); for (@rm_order) { if ($must_keep{$_}) { print "Can't remove $pkg - depends on $_ which is a must k +eep\n"; next PACKAGE; } } print "Safe to remove $pkg\n"; for (@rm_order) { print "\tRemoving $_\n"; remove_pkg($_); } } sub dependencies { my ($pkg) = @_; # Functionality to return list of dependencies } sub remove_pkg { my ($pkg) = @_; # Functionality to remove a package } { my %seen; my %onstack; my @list; sub how_to_uninstall { my $target = shift; (@list, %seen, %onstack) = (); _traverse($target); return @list; } sub _traverse { my $x = shift; $seen{$x} = $onstack{$x} = 1; for my $y (@{$tree{$x}}) { die "cyclic!" if $onstack{$y}; _traverse($y) unless $seen{$y}; } push @list, $x; $onstack{$x} = 0; } }

Cheers - L~R

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2021-10-18 05:08 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (72 votes). Check out past polls.