Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use Data::Dumper;
- my $depends={};
- my $explicit={};
- my $installed={};
- sub needed(){
- my %res;
- my @items=keys %$explicit;
- my $name;
- while($name = shift @items){
- for(@{ $depends->{$name} }){
- push @items,$_ unless $res{$_};
- $res{$_}=1;
- }
- }
- %res
- }
- sub install($$){
- my($name,$is_explicit)=@_;
- if($installed->{$name}){
- print " $name is already installed.\n" if $is_explicit;
- return;
- }
- for(@{ $depends->{$name} }){
- install($_,0);
- }
- print " Installing $name\n";
- $installed->{$name}=1;
- $explicit->{$name}=1 if $is_explicit;
- }
- sub remove($$){
- my($name,$is_explicit)=@_;
- if(! $installed->{$name}){
- print " $name is not installed.\n" if $is_explicit;
- return;
- }
- if($explicit->{$name} and not $is_explicit){
- return;
- }
- my %needed=needed;
- if($needed{$name}){
- print " $name is still needed.\n" if $is_explicit;
- return;
- }
- print " Removing $name\n";
- delete $installed->{$name};
- delete $explicit->{$name};
- for(@{ $depends->{$name} }){
- remove($_,0);
- }
- }
- while(<DATA>){
- print; chomp;
- /^DEPEND ([^ ]+) (.*)$/ and do{
- my($n,$deps)=($1,$2);
- $depends->{$n}||=[];
- push @{$depends->{$n}},$_ foreach split / /,$deps;
- };
- /^INSTALL ([^ ]+)$/ and do{
- install($1,1);
- };
- /^REMOVE ([^ ]+)$/ and do{
- remove($1,1);
- };
- /^LIST$/ and do{
- for(sort keys %$installed){
- print " $_\n";
- }
- };
- }
- #print Dumper $depends;
- __DATA__
- DEPEND TCPIP NETCARD
- DEPEND TELNET TCPIP SOCKET
- DEPEND DNS TCPIP
- DEPEND HTML REGEX XML
- DEPEND REGEX PARSING
- DEPEND BROWSER DNS TCPIP HTML CSS
- INSTALL TCPIP
- REMOVE NETCARD
- REMOVE TCPIP
- REMOVE NETCARD
- INSTALL TCPIP
- LIST
- INSTALL TCPIP
- INSTALL foo
- REMOVE TCPIP
- INSTALL NETCARD
- INSTALL TCPIP
- REMOVE TCPIP
- LIST
- INSTALL TCPIP
- INSTALL NETCARD
- REMOVE TCPIP
- LIST
- REMOVE NETCARD
- INSTALL BROWSER
- LIST
- REMOVE BROWSER
- LIST
- INSTALL HTML
- INSTALL TELNET
- REMOVE SOCKET
- INSTALL DNS
- INSTALL BROWSER
- REMOVE NETCARD
- LIST
- REMOVE BROWSER
- LIST
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement