++ed by:
Syohei Yoshida


Graphviz::DSL - Graphviz Perl interface with DSL


  use Graphviz::DSL;

  my $graph = graph {
      name 'Sample';

      route main => [qw/init parse cleanup printf/];
      route init => 'make', parse => 'execute';
      route execute => [qw/make compare printf /];

      nodes colorscheme => 'piyg8', style => 'filled';

      my $index = 1;
      for my $n ( nodeset() ) {
          node($n->id, fillcolor => $index++);

      edges arrowhead => 'onormal', color => 'magenta4';
      edge ['main' => 'printf'], arrowtail => 'diamond', color => '#3355FF';
      global bgcolor => 'white';

      node 'execute', shape => 'Mrecord',
                      label => '{<x>execute | {a | b | c}}';
      node 'printf',  shape => 'Mrecord',
                      label => '{printf |<y> format}';

      edge ['execute:x' => 'printf:y'];
      rank 'same', 'cleanup', 'execute';

      subgraph {
          global label => 'SUB';
          node 'init';
          node 'make';

      subgraph {
          global label => 'SUB2';
          multi_route +{
              'a' => [qw/b c d/],
              'd' => 'e',
              'f' => {
                  'g' => { 'h' => 'i'},
                  'j' => 'k',

  $graph->save(path => 'output', type => 'png', encoding => 'utf-8');


Graphviz::DSL is Perl version of Ruby gem Gviz. This module provide DSL for generating DOT file(and image if you install Graphviz dot command). Outputted DOT file may be similar to your DSL, because Graphviz::DSL try to keep objects order in DSL(Order of objects in DSL is very important. If you change some objects order, then output image may be changed).


Method in DSL

name $name

Set $name as graph name. Default is 'G'.

type $type

Set $type as graph type. $type should be digraph(directed graph) or graph(undirected graph). Default is 'digraph'.

add, route

Add nodes and them edges. route is alias of add function. You can call these methods like following.

add $nodes

Add $nodes to this graph. $nodes should be Scalar or ArrayRef.

add $node1, \@edges1, $node2, \@edges2 ...

Add nodes and edges. $noden should be Scalar or ArrayRef. For example:

    add [qw/a b/], [qw/c d/]

Add node a and b and add edge a->c, a->d, b->c, b->d.


Add multiple routes at once.

    multi_route +{
        a => [qw/b c/],
        d => 'e',
        f => {
            g => { h => 'i'},
            j => 'k',

equals to following:

    route a => 'b', a => 'c';
    route d => 'e';
    route f => 'g', f => 'j';
    route g => 'h';
    route h => 'i';
    route j => 'k';

node($node_id, [%attributes])

Add node or update attribute of specified node.

edge($edge_id, [%attributes])

Add edge or update attribute of specified edge.


Update attribute of all nodes.


Update attribute of all edges.


Return registered nodes.


Return registered edges.


Update graph attribute.


Set rank.


Create subgraph.

Class Method


Save graph as DOT file.

%args is:


Basename of output file.


Output image type, such as png, gif, if you install Graphviz(dot command). If dot command is not found, it generate only dot file. Graphviz::DSL don't output image if you omit this attribute.


Encoding of output DOT file. Default is utf-8.


Return DOT file as string. This is same as stringify itself. Graphviz::DSL overload stringify operation.


Gviz https://github.com/melborne/Gviz

Graphviz http://www.graphviz.org/


Syohei YOSHIDA <syohex@gmail.com>


Copyright 2013- Syohei YOSHIDA


This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Hosting generously
sponsored by Bytemark