CPAN-Flatten

 view release on metacpan or  search on metacpan

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use strict;
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.303 second using v1.01-cache-2.11-cpan-fd5d4e115d8 )