Graph::Flowchart - Generate easily flowcharts as Graph::Easy objects
use Graph::Flowchart; my $flow = Graph::Flowchart->new(); print $flow->as_ascii();
This module lets you easily create flowcharts as Graph::Easy objects. This means you can output your flowchart as HTML, ASCII, Boxart (unicode drawing) or SVG.
The nodes constructed by the various add_* methods will set the subclass of the node according to the following list:
add_*
The start block.
The end block, created by finish().
finish()
Orindary code blocks, f.i. from $b = 9;.
$b = 9;
Blocks for the various constructs for conditional and loop constructs.
For sub routine declarations.
For use, no and require statements.
use
no
require
Blocks for the various constructs for jumps/returns.
Classes for edges of the true and false if-branches, and for goto, as well as sub routine calls.
Each class will get some default attributes, like if constructs having a diamond-shape.
if
You can override the graph appearance most easily by changing the (sub)-class attributes:
my $chart = Graph::Flowchart->new(); $chart->add_block('$a = 9;'); $chart->add_if_then('$a == 9;', '$b = 1;'); $chart->finish(); my $graph = $chart->as_graph();
Now $graph is a Graph::Easy object and you can manipulate the class attributes like so:
$graph
Graph::Easy
$graph->set_attribute('node.if', 'fill', 'red'); $graph->set_attribute('edge.true', 'color', 'green'); print $graph->as_html_file();
This will color all conditional blocks red, and edges that represent the true branch green.
true
Exports nothing.
All block-inserting routines on the this model will insert the block on the given position, or if this is not provided, on the current position. After inserting the blocks, the current position will be updated.
In addition, the newly inserted block(s) might be merged with blodcks at the current position.
my $grapher = Graph::Flowchart->new();
Creates a new Graph::Flowchart object.
Graph::Flowchart
my $graph = $grapher->as_graph();
Return the internal data structure as Graph::Easy object.
print $grapher->as_ascii();
Returns the flow chart as ASCII art drawing.
print $grapher->as_boxart();
Returns the flow chart as a Unicode boxart drawing.
print $grapher->as_html_file();
Returns the flow chart as entire HTML page.
my $insertion = $grapher->current_block(); # get $grapher->current_block( $block); # set
Get or set the current block in the flow chart, e.g. where new code blocks will be inserted by the add_* methods.
Needs a Graph::Flowchart::Node as argument, which is usually an object returned by one of the add_* methods.
Graph::Flowchart::Node
current() is an alias for current_block().
current()
current_block()
$grapher->make_current($block);
Set the given block as current, and convert it to a joint.
my $first = $grapher->first_block(); # get $grapher->first_block( $block ); # set
Get or set the first block in the flow chart, usually the 'start' block.
my $last = $grapher->last_block(); # get $grapher->last_block( $block); # set
Get or set the last block in the flow chart, usually the block where you last added something via one of the add_* routines.
The returned block will only be the last block if you call finish() beforehand.
my $start = $grapher->start_node();
Returns the START node. See also first_block().
my $end = $grapher->end_node();
Returns the END node. See also last_block().
my $last = $grapher->finish( $block ); my $last = $grapher->finish( );
Adds an end-block. If no parameter is given, uses the current position, otherwise appends the end block to the given $block. See also current_block. Will also update the position of last_block to point to the newly added block, and return this block.
$block
current_block
last_block
my $block = $grapher->new_block( $text, $type ); my $block = $grapher->new_block( $text, $type, $label );
Creates a new block from the given text and type. The type is one of the N_* from Graph::Flowchart::Node.
N_*
The optional label gives the label name, which can be used by goto constructs as target node. See also find_target().
find_target()
my $target = $grapher->find_target( $label );
Given the label $label, find the block that has this text as label and returns it. Returns undef if the block doesn't exists yet.
$label
$grapher->add_group($group_name);
Add a group to the flowchart, and set it as current.
$grapher->no_group();
Forget the current group.
my $current = $grapher->add_block( $block ); my $current = $grapher->add_block( $block, $where );
Add the given block. See new_block on creating the block before hand.
new_block
The optional $where parameter is the point where the code will be inserted. If not specified, it will be appended to the current block, see current_block.
$where
Returns the newly added block as current.
Example:
+---------+ --> | $a = 9; | --> +---------+
my $new = $grapher->add_new_block( $text, $type, $label, $where);
Creates a new block, and adds it to the flowchart. Might merge the new block into the current one, and then returns the new current block.
my $joint = $grapher->add_new_joint(); my $joint = $grapher->add_new_joint($where);
Is a shortcut for add_block(new_block('', N_JOINT())) and creates and adds a joint to the flowchart. The optional parameter $where takes the block where to attach the join to.
add_block(new_block('', N_JOINT()))
my $new = $grapher->insert_block($block, $where);
Insert a block to the current (or $where) block. Any outgoing connections from $where are moved to the new block (unless the blocks are merged).
my $new = $grapher->insert_new_block($where);
A short cut for:
my $block = $grapher->new_block( ... ); my $new = $grapher->insert_block($block, $where);
See insert_block().
insert_block()
my $new = $grapher->insert_new_joint($where);
my $joint = $grapher->add_joint( ... ); my $new = $grapher->insert_block($joint, $where);
my $edge = $grapher->connect( $from, $to ); my $edge = $grapher->connect( $from, $to, $edge_label ); my $edge = $grapher->connect( $from, $to, $edge_label, $edge_class );
Connects two blocks with an edge, setting the optional edge label and edge class.
Returns the Graph::Easy::Edge object for the connection.
Graph::Easy::Edge
$grapher->merge_blocks($first,$second);
If possible, merge the given two blocks into one block, keeping all connections to the first, and all from the second. Any connections between the two blocks is dropped.
+---------+ +---------+ --> | $a = 9; | --> | $b = 2; | --> +---------+ +---------+
This will be turned into:
+---------+ --> | $a = 9; | --> | $b = 2; | +---------+
$grapher->cleanup_joints();
Is called automatically by finish(). This will collapse any left-over joint nodes:
+---+ +-------+ -- false --> | * | -- next --> | $b++; | --> +---+ +-------+
Will be turned into:
+-------+ -- false, next --> | $b++; | --> +-------+
Note that the following routines will not work when used recursively, because they add the entire structure already connect, at once.
If you want a if-then-else, which contains another if-then-else, for instance, you need to construct the blocks first, and then connect them manually.
Pleasee Devel::Graph on how to do this.
Devel::Graph
my $current = $grapher->add_if_then( $if, $then); my $current = grapher->add_if_then( $if, $then, $where);
Add an if-then branch to the flowchart. The optional $where parameter defines at which block to attach the construct.
Returns the new current block, which is a joint.
joint
false +--------------------------------------------+ | v +-------------+ true +---------+ --> | if ($a = 9) | ------> | $b = 1; | -------> * --> +-------------+ +---------+
my $current = $grapher->add_if_then_else( $if, $then, $else); my $current = $grapher->add_if_then_else( $if, $then, $else, $where);
Add an if-then-else branch to the flowchart.
The optional $where parameter defines at which block to attach the construct.
+-------------+ | $b = 2; | --------------------------+ +-------------+ | ^ | | false | | v +-------------+ true +---------+ --> | if ($a = 9) | ------> | $b = 1; | --> * --> +-------------+ +---------+
If $else is not defined, works just like add_if_then().
$else
add_if_then()
my ($current,$body,$continue) = $grapher->add_for( $init, $while, $cont, $body, $continue); my ($current,$body,$continue) = $grapher->add_for( $init, $while, $cont, $body, $continue, $where);
Add a for (my $i = 0; $i < 12; $i++) { ... } continue {} style loop.
for (my $i = 0; $i < 12; $i++) { ... } continue {}
This routine returns three block positions, the current block (e.g. after the loop), the block of the loop body and the position of the (optional) continue block.
+--------------------+ false --> | for: $i < 10; | -------> * --> +--------------------+ | ^ | true +----+ v | +---------------+ +--------+ | $a++; | --> | $i++ | +---------------+ +--------+
my ($current,$body,$continue) = $grapher->add_foreach( $list, $body, $cont); my ($current,$body,$continue) = $grapher->add_foreach( $list, $body, $cont, $where);
Add a for my $var (@lies) { ... } style loop.
for my $var (@lies) { ... }
+----------------------+ false --> | for my $i (@list) | -------> * --> +----------------------+ | ^ | true +----+ v | +---------------+ +--------+ | $a++; | --> | $i++ | # body and continue block +---------------+ +--------+
my ($current,$body, $cont) = $grapher->add_while($while, $body, $cont, $where) = @_;
To skip the continue block, pass $cont as undef.
$cont
This routine returns three block positions, the current block (e.g. after the loop), the block of the loop body and the continue block.
Example of a while loop with only the body (or only the continue block):
continue
+----------------------+ false --> | while ($b < 19) | -------> * --> +----------------------+ | ^ | true | v | +-----------------+ | | $b++; |--+ +-----------------+
Example of a while loop with body and continue block (note similiarity to for loop):
+--------------------+ false --> | while ($i < 10) | -------> * --> +--------------------+ | ^ | true +----+ v | +---------------+ +--------+ | $a++; | --> | $i++ | +---------------+ +--------+
my ($current,$body, $cont) = $grapher->add_until($until, $body, $cont, $where) = @_;
Works just like while, but reverses the true and false edges to represent a until () BLOCK continue BLOCK loop.
false
until () BLOCK continue BLOCK
See also add_while().
add_while()
my $jump = $grapher->add_jump ( $text, $type, $label, $target); my ($jump,$target) = $grapher->add_jump ( $text, $type, $label);
Adds a jump block, with a connection to $target. If $target is just the label name, will try to find a block with that label. If no block can be found, will create it.
$target
The type is one of:
goto break return last next continue
my $joint = $grapher->add_joint( @blocks );
Adds a joint (an unlabeled, star-shaped node) to the flowchart and then connects each block in the given list to that joint. This is used f.i. by if-then-else constructs that need a common joint where all the branches join together again.
When adding a block right after a joint, they will be merged together and the joint will be effectively replaced by the block.
--> * -->
Graph::Easy, Devel::Graph.
This library is free software; you can redistribute it and/or modify it under the same terms of the GPL version 2 or later. See the LICENSE file for information.
Copyright (C) 2004-2007 by Tels http://bloodgate.com
To install Graph::Flowchart, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Graph::Flowchart
CPAN shell
perl -MCPAN -e shell install Graph::Flowchart
For more information on module installation, please visit the detailed CPAN module installation guide.