Graphing module usage

BooK on 2007-10-19T10:20:38

This is a really quick hack. I wanted to see which modules used which in my debugging session. This is exactly what Devel::TraceUse does.

I wanted to be able to see those relationship and be able to doodle on a piece of paper and stuff. So, here goes, thanks to GraphViz:

#!/usr/bin/env perl
use strict;
use warnings;
use GraphViz;

my $png = shift || 'use.png';
my $g = GraphViz->new( rankdir => 1 );

my @stack;
while (<>) {
    # first node
    /^Modules used from (.*):/ && do {
        $g->add_node($1);
        @stack = ($1);
    };
    # all other nodes
    /^((?:  )+)(\S+),/ && do {
        my $idx = length($1) / 2;
        $stack[$idx] = $2;
        $g->add_node($2);
        $g->add_edge( $stack[ $idx - 1 ] => $2 );
    };

    # ignore all other lines
}

$g->as_png($png);


Re: Graphing module usage

cosimo on 2007-10-19T11:20:45

Cool!
I had done something like that for inheritance graphs (I know, nothing new).
What do you feed this script with?

simple format

educated_foo on 2007-10-20T21:36:36

## Or, since DOT is a simple format, the module-free version:

#!/usr/bin/env perl

$png = shift || 'use.png';
open O, "| dot -Tpng > $png" or die $!;

print O "digraph g {\n";
while (<>) {
    if (/^Modules used from (.*):/) {
        @stack = ($1);
    } elsif (/^((?:  )+)(\S+),/) {
        $idx = length($1) / 2;
        $stack[$idx] = $2;
        print O qq{"$stack[$idx-1] -> $2;\n"};
    }
}
print O "}\n";
close O;