The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


GraphViz2::Parse::STT - Visualize a Set::FA::Element state transition table as a graph


    use GraphViz2::Parse::STT;
    use File::Slurp; # For read_file().
    my $stt = read_file('sample.stt.1.dat');
    # no objects - quicker
    my $gd = GraphViz2::Parse::STT::to_graph($stt);

    # populate a GraphViz2 object with a Graph::Directed of a parser
    my $gv = GraphViz2->from_graph(GraphViz2::Parse::STT::graphvizify($gd));

    # visualise with another mode
    $gd = GraphViz2::Parse::STT::graphvizify($gd, 're_nodes'); # or 're_edges'

    # OO interface, using lazy-built attributes
    my $gvp = GraphViz2::Parse::STT->new(stt => $stt, mode => 're_structs');
    my $gd = $gvp->as_graph; # Graph::Directed object
    # or supply a suitable Graph::Directed object
    my $gvp = GraphViz2::Parse::STT->new(as_graph => $gd);
    # then get the GraphViz2 object
    my $gv = $gvp->graph;

    # DEPRECATED ways to get $gvp with populated $gv
    my $gvp = GraphViz2::Parse::STT->new;
    $gvp->create(stt => $stt);
    my $gv = $gvp->graph;
    # or give it a pre-set-up GraphViz2 object
    my $gv = GraphViz2->new(...);
    my $gvp = GraphViz2::Parse::STT->new(graph => $gv);
    # call ->create as above

    # produce a visualisation
    my $format = shift || 'svg';
    my $output_file = shift || "output.$format";
    $gv->run(format => $format, output_file => $output_file);

See t/gen.parse.stt.t.

Note: t/sample.stt.2.dat is output from Graph::Easy::Marpa::DFA V 0.70, and can be used instead of t/sample.stt.1.dat in the above code.


Takes a Set::FA::Element-style state transition table and converts it into a Graph::Directed object, or directly into a GraphViz2 object.


This is the recommended interface.


    my $gd = GraphViz2::Parse::STT::to_graph($stt);

Given STT text, returns a Graph::Directed object describing the finite state machine for it.

The nodes are all states, and the edges are regular expressions that cause a transition to another state.


    my $gv = GraphViz2->from_graph(GraphViz2::Parse::STT::graphvizify($gd, $mode));

Mutates the given graph object to add to it the graphviz attributes visualisation "hints" that will make the "from_graph" in GraphViz2 method visualise this regular expression in the most meaningful way, including labels and groupings.

It is idempotent, but in re_nodes mode, it deletes the transition edges and replaces them with additional nodes and edges.

If a second argument is given, it will be the visualisation "mode". The default is re_structs. Also available is re_nodes, and re_edges where the regular expressions are simply added as labels to the state-transition edges.

Returns the graph object for convenience.


This is a Moo class, but with a recommended functional interface.

Constructor attributes


Text with a state transition table, with a Perl-ish list of arrayrefs, each with 3 elements.

That is, it is the contents of the arrayref 'transitions', which is one of the keys in the parameter list to Set::FA::Element's new().

A quick summary of each element of this list, where each element is an arrayref with 3 elements:

o [0] A state name
o [1] A regexp
o [2] Another state name (which may be the same as the first)

The DFA in Set::FA::Element tests the 'current' state against the state name ([0]), and for each state name which matches, tests the regexp ([1]) against the next character in the input stream. The first regexp to match causes the DFA to transition to the state named in the 3rd element of the arrayref ([2]).

See t/sample.stt.1.dat for an example.

This key is optional. You need to provide it by the time you access either the "as_graph" or "graph".


The Graph::Directed object to use. If not given, will be lazily built on access, from the "stt".


The GraphViz2 object to use. This allows you to configure it as desired.

This key is optional. If provided, the create method will populate it. If not, it will have these defaults, lazy-built and populated from the "as_graph".

    my $gv = GraphViz2->new(
            edge   => {color => 'grey'},
            global => {directed => 1},
            graph  => {rankdir => 'TB'},
            node   => {color => 'blue', shape => 'oval'},


The mode to be used by "graphvizify".

create(regexp => $regexp, mode => $mode)

DEPRECATED. Mutates the object to set the stt attribute, then accesses the as_graph attribute (possibly lazy-building it), then graphvizifys its as_graph attribute with that information, then from_graphs its graph.

Returns $self for method chaining.


Many thanks are due to the people who chose to make Graphviz Open Source.

And thanks to Leon Brocard, who wrote GraphViz, and kindly gave me co-maint of the module.


GraphViz2 was written by Ron Savage <> in 2011.

Home page:


Australian copyright (c) 2011, Ron Savage.

All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Perl License, a copy of which is available at: