Graphing Perl

Document Sample
scope of work template
							  Graphing Perl
       Leon Brocard
leon@iterative-software.com
     Iterative Software Ltd.
Introduction

"Did you ever see such a thing as a drawing of a
muchness?"
-- Dormouse, Alice in Wonderland


We will cover:
   an introduction to graphs and GraphViz
   the visualisation of various parts of Perl using directed
    graphs
Why?

A picture is worth a thousand words

Text bad, diagrams pretty

Spatial memory

Spreadsheets

Hierarchies are everywhere (i.e. "I can graph that!")
Graphs not charts!

This talk is about graphs not charts!
Example graph
Graphs?

What is a graph?
   A collection of nodes linked together with edges

What is a directed graph?
   A graph -- but the edges have a direction
How?

GraphViz
   automatic graph layout and drawing package from
   AT&T
   simple text file input
   variety of graphing outputs (PostScript, PNG, etc.)
   fairly fast (~2 secs)
   http://www.graphviz.org/

GraphViz module
   on the CPAN
   hides the creation of the text file
   simple OO interface to producing graphs
   all graphs shown here produced using it
Example GraphViz input

use GraphViz;

my $g = GraphViz->new();

$g->add_node(’London’);
$g->add_node(’Paris’, label => "City of\nlurve");
$g->add_node(’New York’);

$g->add_edge(’London’ => ’Paris’);
$g->add_edge(’London’ => ’New York’,
             label => ’Far’);
$g->add_edge(’Paris’ => ’London’);

print $g->as_png;
Example GraphViz output
Data structures

Programs consist of data structures and code.
Data::Dumper can help:
$VAR1 = [
            ’red’,
            {
              ’a’ => [
                         3,
                         1,
                         4,
                         1
                    ],
             ’b’ => {
                      ’w’ => ’b’,
                      ’q’ => ’a’
                    }
         },
         ’blue’,
          undef
       ];
Data structures graph

my $graph = GraphViz::Data::Grapher->new(\@d);
print $graph->as_png;
Regular expressions

Regexes
   a powerful part of Perl
   an entirely different mini language
   tricky to understand at a glance

Perl regex debugger
    use re "debug";
    graph regex bytecode
Regular expressions graph


my $g = GraphViz::Regex->new(’^([abcd0-9]+|foo)’);
print $g->as_png;
XML

XML
   is a buzzword
   represents data in a tree
   hard for humans to understand
 <html><head><title>news</title></head><body>
 <p>Check out the <a href="/news/">latest news</a>.</p
 <p>Under construction!!!</p></body></html>

 my $graph = GraphViz::XML->new($xml);
 print $graph->as_png;
XML graph
DBI

DBI
      very popular
      databases are everywhere
      many tables
      many fields
      complex relationships

GraphViz::DBI (by Marcel Grunauer) graphs tables and
their relations

It guesses: if there is a table called "product" and another
table contains a field called "product_id", then it assumes
the two are related
DBI graph
Subroutine cross-reference

Back to code: How can subroutines in a module call each
other?

B::Xref (by Malcolm Beattie)
    hooks into Perl compiler
    documents possible call paths
Subroutine cross-reference graph




Remember: possible call paths
Code profiling (line level)

Better than potential code graphs? Code profiling

Devel::SmallProf (by Ted Ashton)
   perl-line Perl profiler
   modified to produce graphs

Following graph of a program by Greg McCarroll
    hot spot is clearly visible
Code profiling (line level) graph
Code profiling (subroutine level)

Line-level profiling useless for large programs

Devel::DProf
   subroutine-level profiling
   collect call graph information
   a little glue
Code profiling (subroutine level)
Modules

How have modules called each other?

... also possible from Devel::DProf
Modules graph
ISA hierarchies

GraphViz::ISA (by Marcel Grunauer) can graph @ISA
hierarchies at run-time

Example from Damian Conway’s "Object Oriented Perl"
CPAN dependencies

ppmgraph (by Marcel Grunauer) can graph CPAN tarball
dependencies
Opcodes

Opcodes
   Programs are compiled into parse trees and opcodes,
   which the Perl interpreter then follows
   These are quite daunting to Perl internals beginners
   B::Graph (by Stephen McCamant)

  perl -MO=Graph,-dot -e ’$a = 1; $b
  = 2; print $a+$b’
Opcodes graph
Opcodes 2

B::Tree (by Simon Cozens) is simpler:

  perl -MO=Tree -e ’$a = 1; $b = 2;
  print $a+$b’
Parse::RecDescent

Parse::RecDescent (by Damian Conway)
    powerful recursive descent text parser generator
    writing grammars is tricky
    visualisation aids understanding and can find bugs

 my $g = GraphViz::Parse::RecDescent->new($grammar);
 print $g->as_png;
Parse::RecDescent graph

examples/demo_operator.pl:
The Perl grammar

Stephane Payrard and Francois Desarmenien have tried to
graph the Perl grammar

This describes the components of a Perl script
The Perl grammar graph
Overview and future

Graphing is Good

Graphing Perl is possible and easy (mostly glue)

The GraphViz module is available on the CPAN

What else can we graph?

Large graphs problem: interactive? fish-eye?

... in my Copious Spare Time ;-)

						
Related docs
Other docs by hcw25539
Programmierkurs FORTRAN 95
Views: 141  |  Downloads: 0
Pascal Berruet
Views: 25  |  Downloads: 0
Pascal Vuillemin 15Q3
Views: 4  |  Downloads: 0
Lycée Polyvalent Blaise PASCAL
Views: 92  |  Downloads: 0