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...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.