Text::Tree - format a simple tree of strings into a textual tree graph
use Text::Tree; my $tree = new Text::Tree( "root", [ "left\nnode" ], [ "right", [ "1" ], [ "2" ] ] ); print $tree->layout("boxed"); __OUTPUT__ +----+ |root| +----+ .---^---. +----+ +-----+ |left| |right| |node| +-----+ +----+ .-^-. +-+ +-+ |1| |2| +-+ +-+
my $tree = new Text::Tree( "label", [ "left child label", [ ... ] ], [ "right child label", [ ... ] );
Create a new tree object from a nested set of array references. The first element of each array must be a string used as a node label. The remaining elements must each be an array reference for a child of the node. Labels may contain newlines to support multiple lines of text.
my @lines = $tree->layout( "centered in boxes" ); print @lines;
Lays out the tree into an array of newline-terminated strings, ready for printing or displaying. The optional style argument may contain various keywords such as 'center', 'box', 'line', 'oval' and/or 'space'. These style keywords affect how the tree nodes are formatted.
Allows the caller to develop a tree structure, using nested arrays of strings and references. Once developed, the whole tree can be printed as a diagram, with the root of the tree at the top, and child nodes formatted horizontally below them.
The string labels are printed as-is, or optionally surrounded with a simple outlining style using printable ASCII characters.
This module may be used with object-oriented or simple function calls.
Mark Jason Dominus (aka MJD) asked for this functionality on his Expert-level "Perl Quiz of the Week" Number 5. You can find out more about the QOTW discussion forum at http://perl.plover.com/qotw/
The central formatting routine was submitted by Ron Isaacson to the Quiz forum as one possible solution to the general problem.
Ed Halley adapted the Ron Isaacson entry (with permission), to correct some tree structures not originally handled, and to allow more formatting options for the box styles.
Copyright 2003-2004 by Ron Isaacson
Portions Copyright 2003 by Mark Jason Dominus
Portions Copyright 2004 by Ed Halley
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.