package Isanchez; use strict; use Carp; our $VERSION = '0.01'; sub new { my ($class, $file) = @_; my $self = bless {}, $class; $self->_build($file); return $self; } sub _build { my ($self, $file) = @_; croak "Unable to retrieve data" unless open(INPUT,$file); while (my $line = ) { chomp $line; my($id, $parent_id, $title) = split /\s+/ , $line, 3; unless ($title && $id =~ /^\d+$/ && $parent_id =~ /^\d+/ && $parent_id < $id) { carp "Unable to parse\n$line"; next; } $self->{$title}{decendent} = $id; push @{$self->{decendents}{$parent_id}}, $title; } } sub get_decendents { my ($self, $origin) = @_; croak "You must enter an origin in the tree" unless $origin; croak "$origin can not be found" unless exists $self->{$origin}; my $index = $self->{$origin}{decendent}; my @list = @{$self->{decendents}{$index}} if exists $self->{decendents}{$index}; return $origin unless @list; my @decendents = ($origin); while (@list) { my $decendent = shift @list; push @decendents, $decendent; my $index = $self->{$decendent}{decendent}; push @list, @{$self->{decendents}{$index}} if exists $self->{decendents}{$index}; } return @decendents; }