File Coverage

blib/lib/Data/Hierarchy/Traverser.pm
Criterion Covered Total %
statement 15 51 29.4
branch 0 16 0.0
condition 0 16 0.0
subroutine 5 11 45.4
pod 0 1 0.0
total 20 95 21.0


line stmt bran cond sub pod time code
1             package Data::Hierarchy::Traverser;
2              
3 1     1   24081 use 5.008;
  1         4  
  1         39  
4 1     1   5 use Carp;
  1         3  
  1         94  
5 1     1   5 use strict;
  1         6  
  1         43  
6 1     1   4 use warnings;
  1         2  
  1         33  
7 1     1   1396 use Data::Dumper;
  1         11379  
  1         562  
8              
9             require Exporter;
10              
11             our @ISA = qw(Exporter AutoLoader);
12              
13             # Items to export into callers namespace by default. Note: do not export
14             # names by default without a very good reason. Use EXPORT_OK instead.
15             # Do not simply export all your public functions/methods/constants.
16              
17             # This allows declaration use Hierarchy::Traverser ':all';
18             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19             # will save memory.
20             our %EXPORT_TAGS = ( 'all' => [ qw(
21            
22             ) ] );
23              
24             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25              
26             our @EXPORT = qw(
27             hierarchy_traverse
28             );
29              
30             our $VERSION = '0.01';
31              
32             sub hierarchy_traverse {
33 0     0 0   my ($roots, $get_children_function, $options) = @_;
34 0           my $deepth = $options->{deepth};
35 0           my $is_leaf = $options->{is_leaf};
36 0           my $leaf = $options->{leaf};
37 0           my $bare_branch = $options->{bare_branch};
38 0           my $pre_branch= $options->{pre_branch};
39 0           my $post_branch = $options->{post_branch};
40              
41 0 0         $options->{deepth} -- if defined $options->{deepth};
42              
43 0 0         defined $roots or croak "parameter roots is mandatory";
44 0 0         defined $get_children_function or croak "parameter get_children_function is mandatory\n";
45 0 0         UNIVERSAL::isa($get_children_function, 'CODE') or croak "get_children_function must be a sub\n";
46              
47             # should check if $get_children_function is ref to CODE.
48 0   0 0     $is_leaf ||= sub {};
  0            
49 0   0 0     $leaf ||= sub {};
  0            
50 0   0 0     $pre_branch ||= sub {};
  0            
51 0   0 0     $post_branch ||= sub {};
  0            
52 0   0 0     $bare_branch ||= sub {};
  0            
53              
54 0           my @roots;
55 0 0         @roots = ref($roots)? @$roots : ($roots);
56 0           foreach my $node (@roots) {
57 0 0         if ($is_leaf->($node)) {
58 0           $leaf->($node);
59             } else {
60 0           my $children = $get_children_function->($node);
61 0 0 0       if (not defined $children or 0 ==@$children) {
62 0           $bare_branch->($node);
63 0           next;
64             } else {
65 0 0 0       if (defined $deepth and $deepth < 0 ) {
66 0           $bare_branch->($node);
67 0           next;
68             }
69 0           $pre_branch->($node);
70 0           hierarchy_traverse($children, $get_children_function, $options);
71 0           $post_branch->($node);
72             }
73             }
74             }
75             }
76              
77             # Preloaded methods go here.
78              
79             1;
80             __END__