#!/usr/bin/perl use strict; use warnings; use Language::AttributeGrammar; package Leaf; sub new{ return bless { value => $_[1] }, 'Leaf'; } package Branch; sub new{ return bless { value => $_[1], _list => undef }, 'Branch'; } sub add_child{ my $self = shift; push @{ $self->{_list} }, @_; return $self; } sub list { if (@_) { bless { head => $_[0], tail => list(@_[1..$#_]) }, 'Cons'; } else { bless {}, 'Nil'; } } sub children{ return list( @{ $_[0]->{_list} }); }