Text::Tree(3)

πŸ“¦
version 1.0 Β· 2 versions
, in fedora 43

πŸ’– Text::Tree πŸ’–

okay bestie, so this is the manual page for Text::Tree, a lil Perl module from the User Contributed Perl Documentation collection. she takes a simple tree of strings and formats it into a textual tree graph β€” like, actual cute lil boxes and branches made of ASCII, fr fr. 🌳✨


🌸 NAME 🌸

Text::Tree β€” format a simple tree of strings into a textual tree graph. it's giving diagram energy, no cap. πŸ’…


✨ SYNOPSIS ✨

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|
       +-+ +-+

like, look at her. she did that. πŸ‘‘ the ASCII tree is genuinely iconic and i'm obsessed.


πŸŽ€ METHODS πŸŽ€

πŸ’œ new()

my $tree = new Text::Tree( "label",
                           [ "left child label", [ ... ] ],
                           [ "right child label", [ ... ] );

creates a brand new tree object out of a nested set of array references, bestie. the rules are simple:

  • the first element of each array must be a string β€” that's your node label 🏷️
  • every element after that must be an array reference, and each one is a child of that node 🌱
  • labels can contain newlines to support multiple lines of text β€” so go off, multi-line queen πŸ’…

πŸ¦‹ layout()

my @lines = $tree->layout( "centered in boxes" );
print @lines;

lays the whole tree out into an array of newline-terminated strings, ready for printing or displaying. like, it does the whole glow-up for you. ✨

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 get formatted. mix and match, it's your world 🌈


🌼 DESCRIPTION 🌼

allows the caller to develop a tree structure using nested arrays of strings and references. once it's all 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. hierarchy? slay. πŸ“Š

the string labels are printed as-is, or optionally surrounded with a simple outlining style using printable ASCII characters. simple, clean, effective. periodt. πŸ’–

this module may be used with object-oriented or simple function calls. versatility is the vibe, iykyk.


πŸ“œ HISTORY πŸ“œ

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/ β€” go look, bestie, it's lore. πŸ“–

the central formatting routine was submitted by Ron Isaacson to the Quiz forum as one possible solution to the general problem. king behavior honestly. πŸ‘‘

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. glow-up achieved. ✨


copyright stuff, bestie, gotta respect it:

  • 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. so like, free as in freedom, not free as in "no rules". 🫢


this page ships with perl v5.42.0 and it's dated 2025-07-25 πŸ’–

and that's the whole page, bestie!! go build a tree, format it in boxes, and be the main character of your own terminal. mwah πŸ’‹ 🌈 ⭐ πŸŽ€

πŸ“¦ where this page comes from