        package ExampleTree4;

        # Create a new tree object
        # arguments: ->(label of root node,
        #               [arguments for child 1],
        #               [arguments for child 2],
        #               ...);
        sub new {
          my ($pack, $label, @subnodes) = @_;
          my @subobjects = map $pack->new(@$_), @subnodes;
          bless [$label, @subobjects] => $pack;
        }

        # If the label of the tree node is 'foo',
        # we format it like this:
        #    +-----+
        #    | foo |
        #    +-----+
        sub text {
          my $self = shift;
          my $label = $self->[0];
          my $ln = length $label;
          $label .= " " x (5 - $ln) if $ln < 5;  # pad with spaces to width 5
          $ln = length $label;
          my $top = "+" . "-" x $ln . "+";
          return [$top, "|$label|", $top];
        }

        sub children {
          my $self = shift;
          my @children = @$self;
          shift @children;  # throw away the label
          @children;
        }

1;

