Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

BooK (2612)

BooK
  {book} {at} {cpan.org}
http://paris.mongueurs.net/
Yahoo! ID: philippe_bruhat (Add User, Send Message)

Obfuscation [plover.com]. Pink [axis-of-aevil.net]. HTTP::Proxy [cpan.org]. YEF [yapceurope.org]. Fishnet [perl.org]. Kapow [cpan.org]. Cog's [perl.org] bitch [cpan.org]. Invitation [perl.org]. White [perl.org] Camel [perl.org]. Nuff' said.

Journal of BooK (2612)

Friday October 19, 2007
05:20 AM

Graphing module usage

[ #34717 ]

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);

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Cool!
    I had done something like that for inheritance graphs (I know, nothing new).
    What do you feed this script with?
  • ## 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"};
        }
    }
    pri