-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeWalk.pm
80 lines (56 loc) · 1.68 KB
/
TreeWalk.pm
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package TreeWalk;
use strict;
use warnings;
use feature "state";
binmode STDERR, ':utf8';
my $DEBUG="false";
my $debug = sub {
my $message = shift @_;
print STDERR $message . "\n" if $DEBUG eq "true";
};
=comment
description:
the function walks the directory structure and returns a list of subtrees. it also returns a signal/hint in which direction the position inside the tree
changed.
implementation:
if the root has not been already set it is by taking the passed node.
signals:
CURRENT - the function is returning the current node without moving
up or down inside the tree
UP - the function is returning a child of a previous parent
DOWN - the function is returning a parent of a previous child
=cut
our @node_list;
sub walk {
my $node = shift @_;
my $signal;
if ( defined $node->{ "child" }
and scalar keys %{ $node->{ "child" } } ){
$signal = "UP";
push @node_list, $signal;
$debug->("Signal $signal.");
$debug->("Processing children, going down inside the tree.");
}
push @node_list, $node;
$debug->("Pushed ".$node->{"title"}." to list.");
my @child_list = keys %{ $node->{ "child" } };
while ( @child_list ){
my $child_id = shift @child_list;
$debug->("processing child $child_id.");
walk( \%{ $node->{ "child" }->{ $child_id } } );
if ( @child_list ){
$signal = "CURRENT";
push @node_list, $signal;
$debug->("Signal $signal.");
$debug->("Processing next child.");
}
}
if ( defined $node->{ "child" }
and scalar keys %{ $node->{ "child" } } ){
$signal = "DOWN";
push @node_list, $signal;
$debug->("Signal $signal.");
$debug->("Children processed, going up inside the tree.");
}
}
1;