NAME
Graph::Easy::Parser - Parse graph from textual description
SYNOPSIS
# creating a graph from a textual description
use Graph::Easy::Parser;
my $parser = Graph::Easy::Parser->new();
my $graph = $parser->from_text(
'[ Bonn ] => [ Berlin ]'.
'[ Berlin ] => [ Rostock ]'.
);
print $graph->as_ascii( );
DESCRIPTION
Graph::Easy::Parser
lets you parse simple textual descriptions of graphs, and constructs a Graph::Easy
object from them.
The resulting object can than be used to layout and output the graph.
Input
The input consists of text describing the graph.
[ Bonn ] --> [ Berlin ]
[ Frankfurt ] <=> [ Dresden ]
[ Bonn ] --> [ Frankfurt ]
[ Bonn ] ==> [ Frankfurt ]
See Output for how this will be rendered in ASCII art.
The edges between the nodes can have the following styles:
--> solid
==> double
..> dotted
- > dashed
~~> wave
.-> dot-dash
..-> dot-dot-dash
In additon the following three directions are possible:
--> connect the node on the left to the node on the right
<--> the direction between the nodes
goes into both directions at once
<-- connect the node on the right to the node on the left
Of course you can combine all three directions with all styles.
Output
The output will be a Graph::Easy object, see there for what you can do with it.
EXAMPLES
See Graph::Easy for an extensive list of examples.
METHODS
Graph::Easy::Parser
supports the following methods:
new()
use Graph::Easy::Parser;
my $parser = Graph::Easy::Parser->new();
Creates a new parser object.
reset()
$parser->reset();
Reset the status of the parser, clear errors etc.
from_text()
my $graph = $parser->from_text( $text );
Create a Graph::Easy object from the textual description in $text
.
Returns undef for error, you can find out what the error was with error().
This method will reset any previous error, and thus the $parser
object can be re-used to parse different texts by just calling from_text()
multiple times.
from_file()
my $graph = $parser->from_file( $filename );
my $graph = Graph::Easy::Parser->from_file( $filename );
Creates a Graph::Easy object from the textual description in the file $filename
.
The second calling style will create a temporary Graph::Easy::Parser object, parse the file and return the resulting Graph::Easy object.
Returns undef for error, you can find out what the error was with error() when using the first calling style.
error()
my $error = $parser->error();
Returns the last error, or the empty string if no error occured.
parse_error()
$parser->parse_error( $msg_nr, @params);
Sets an error message from a message number and replaces embedded templates like ##param1##
with the passed parameters.
valid_attribute()
my $val = $parser->valid_attribute ($name, $value);
Checkc that the given name/value pair is an valid attribute, and returns the new value or undef for invalid attributes.
The returned attribute value might differ from what you pass in as:
my $val =
$parser->valid_attribute ('color', 'red');
print $val; # prints '#ff0000'
_parse_attributes()
my $attributes = $parser->_parse_attributes( $txt );
Internal usage only. Takes a text like this:
attribute: value; attribute2 : value2;
and returns a hash with the attributes.
EXPORT
Exports nothing.
SEE ALSO
AUTHOR
Copyright (C) 2004 - 2005 by Tels http://bloodgate.com
See the LICENSE file for information.