use Text::Trie qw(Trie walkTrie);
  @trie = Trie 'abc', 'ab', 'ad';
  walkTrie sub {print("[",shift,"]")}, sub {print(shift)}, sub {print "->"}, 
    sub {print ","}, sub {print "("}, sub {print ")"}, @trie;

Given list of strings returns an array that consists of common heads and tails of strings. Element of an array is a string or an array reference. Each element corresponds to a group of arguments.

Arguments are split into groups according to the first letter. If group consists of one element, it results in the string element in the output array. If group consists of several elements, then the corresponding element of output array looks like

  [$head, ...]

where $head is the common head of the group, the rest is the result of recursive application of Trie to tails of elements of the group.


Takes 6 references to subroutines and an array as arguments. Array is interpreted as output of Trie, the routine walks over the tree depth-first. If element of the array is a reference to array, it is interpreted as a node, and the walkTrie is applied to the corresponding array (without the first element) recursively. Otherwise the element is interpreted as a leaf of the tree.

Subroutines are executed when (in the order of arguments of walkTrie):

  • a leaf of the tree is met, argument is the corresponding element of array;

  • a node is met, argument is the first element of the corresponding array;

  • a node that is not-empty is met, argument is the reference to the corresponding array; is called after the previous one;

  • between two sibling nodes or leafs, argument is the next node or leaf;

  • before executing any other code for a node, argument is the node;

  • after executing any other code for a node, argument is the node;

Any one of the first six arguments can be undef instead of being a reference to a subroutine.


None by default, Trie and walkTrie are @EXPORTABLE.


Variable $Text::Trie::step can be set to a value bigger than 1 to set the length of smallest units arguments can be broken into.


Contact Ilya Zakharevich,