Topological sort of a DOT Graph

Today I needed to get a sequential execution order for a system that I already had a DOT dependency graph of, which I knew contained no cycles.

Currently I don’t officially have access to.. well, anything resembling a development environment at all really, so I was faced with the prospect of doing this in vba. As my masochism is underdeveloped I haven’t actually tried this yet, but given I can barely read vba I’d estimate this task at at least a few hours, with a worst-case blowout of a day.

Obviously I didn’t do that, so below is the code to do it in perl, research time 15 minutes, development time 5 minutes. Let me say that again: 20 minutes, versus a few hours. Multiply that out by the amount of time you hire programmers for and how much you pay them and see how well your “risk of getting a virus mitigated by locking down all IT resources” strategy is stacking up.

use Graph::Reader::Dot;
use Graph;

my $reader = Graph::Reader::Dot->new();
my $graph = $reader->read_graph($ARGV[0]);

my @sorted_nodes = $graph->topological_sort;

foreach(@sorted_nodes) {
  print $_,"\n";
}

Tl;dr Let the people doing the work decide on the tools.

(I used perl instead of ruby because I couldn’t find a gem to read DOT files)