If you're looking for generic dependency-handling code (there are Perl implementations of 'make' I believe; don't re-invent the wheel), something recursive like this might work for you:
my %dependencies = (
item1 => [ 'item2' ],
item2 => [ qw' item3 item4 ' ],
);
my %done;
sub act_on {
my $item = shift;
$done{$item} = undef; # mark it early to avoid circular dependen
+cies
if (exists $dependencies{$item}) {
foreach my $dep (@{$dependencies{$item}}) {
warn "Circular dependency $item => $dep"
if exists $done{$dep} and !$done{$dep};
&act_on($dep) unless exists $done{$dep};
}
}
# Dependencies satisfied, do whatever on $item
print "Doing $item...\n";
$done{$item} = 1; # really done
}
act_on "item1";
Doing item3...
Doing item4...
Doing item2...
Doing item1...
Change the function name around if you like to make it more idiomatic if that's what you're looking for...