CPAN-Flatten

 view release on metacpan or  search on metacpan

lib/CPAN/Flatten/Tree.pm  view on Meta::CPAN

package CPAN::Flatten::Tree;
use strict;
use warnings;
use utf8;
use Scalar::Util 'weaken';

sub new {
    my $class = shift;
    my %args = ref $_[0] ? %{$_[0]} : @_;
    my $self = bless {
        _parent => undef,
        _children => [],
        %args,
    }, $class;
    $self;
}

sub add_child {
    my ($self, $node) = @_;
    if ($node->{_parent}) {
        require Carp;
        Carp::confess("node (@{[$node->uid]}) already has a parent");
    }
    push @{ $self->{_children} }, $node;
    $node->{_parent} = $self;
    weaken $node->{_parent};
    $self;
}

sub is_child {
    my ($self, $that) = @_;
    for my $child ($self->children) {
        return 1 if $child->equals($that);
    }
    return;
}



( run in 0.337 second using v1.01-cache-2.11-cpan-0d8aa00de5b )