/usr/share/perl5/Tree/Simple/Visitor/LoadClassHierarchy.pm is in libtree-simple-visitorfactory-perl 0.15-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | package Tree::Simple::Visitor::LoadClassHierarchy;
use strict;
use warnings;
our $VERSION = '0.15';
use Scalar::Util qw(blessed);
use base qw(Tree::Simple::Visitor);
sub new {
my ($_class) = @_;
my $class = ref($_class) || $_class;
my $visitor = {};
bless($visitor, $class);
$visitor->_init();
return $visitor;
}
sub _init {
my ($self) = @_;
$self->{class_to_load} = undef;
$self->{include_methods} = 0;
$self->SUPER::_init();
}
sub setClass {
my ($self, $class_to_load) = @_;
(defined($class_to_load)) || die "Insufficient Arguments : Must provide a class to load";
$self->{class_to_load} = $class_to_load;
}
sub includeMethods {
my ($self, $boolean) = @_;
$self->{include_methods} = ($boolean ? 1 : 0) if defined $boolean;
return $self->{include_methods};
}
sub visit {
my ($self, $tree) = @_;
(blessed($tree) && $tree->isa("Tree::Simple"))
|| die "Insufficient Arguments : You must supply a valid Tree::Simple object";
# it must be a leaf
($tree->isLeaf()) || die "Illegal Operation : The tree must be a leaf node to load a class hierarchy";
(defined $self->{class_to_load}) || die "Insufficient Arguments : Must provide a class to load";
# get the filter
my $filter = $self->getNodeFilter();
# get the class to load
my $class_to_load = ref($self->{class_to_load}) || $self->{class_to_load};
# deal with the include trunk functionality
if ($self->includeTrunk()) {
$tree->setNodeValue(defined $filter ? $filter->($class_to_load) : $class_to_load);
}
else {
my $new_tree = Tree::Simple->new(defined $filter ? $filter->($class_to_load) : $class_to_load);
$tree->addChild($new_tree);
if ($self->includeMethods()) {
$self->_loadMethods($new_tree, $class_to_load, $filter);
}
$tree = $new_tree;
}
# and load it recursively
$self->_loadClass($tree, $class_to_load, $filter);
}
sub _loadClass {
my ($self, $tree, $class_to_load, $filter) = @_;
my @superclasses;
{
no strict 'refs';
@superclasses = @{"${class_to_load}::ISA"};
}
foreach my $superclass (@superclasses) {
my $new_tree = Tree::Simple->new(defined $filter ? $filter->($superclass) : $superclass);
$tree->addChild($new_tree);
if ($self->includeMethods()) {
$self->_loadMethods($new_tree, $superclass, $filter);
}
$self->_loadClass($new_tree, $superclass, $filter);
}
}
sub _loadMethods {
my ($self, $tree, $class, $filter) = @_;
my @methods;
{
no strict 'refs';
@methods = sort grep { defined &{"${class}::$_"} } keys %{"${class}::"};
}
foreach my $method (@methods) {
$tree->addChild(Tree::Simple->new(defined $filter ? $filter->($method) : $method));
}
}
1;
__END__
=head1 NAME
Tree::Simple::Visitor::LoadClassHierarchy - A Visitor for loading class hierarchies into a Tree::Simple hierarchy
=head1 SYNOPSIS
use Tree::Simple::Visitor::LoadClassHierarchy;
# create an visitor
my $visitor = Tree::Simple::Visitor::LoadClassHierarchy->new();
# set class as an instance, or
$visitor->setClass($class);
# as a package name
$visitor->setClass("My::Class");
# pass our visitor to the tree
$tree->accept($visitor);
# the $tree now mirrors the inheritance hierarchy of the $class
=head1 DESCRIPTION
This visitor will traverse a class's inheritance hierarchy (through the @ISA arrays) and create a Tree::Simple hierarchy which mirrors it.
=head1 METHODS
=over 4
=item B<new>
There are no arguments to the constructor the object will be in its default state. You can use the C<setNodeFilter> method to customize its behavior.
=item B<includeTrunk ($boolean)>
Setting the C<$boolean> value to true (C<1>) will cause the node value of the C<$tree> object passed into C<visit> to be set with the root value found in the class hierarchy. Setting it to false (C<0>), or not setting it, will result in the first value in the class hierarchy creating a new node level.
=item B<includeMethods ($boolean)>
Setting the C<$boolean> value to true (C<1>) will cause methods to be added as a children of the class node. Setting it to false (C<0>), or not setting it, will result in this not happening.
B<NOTE:> Methods are sorted ascii-betically before they are added to the tree. This allows a more predictable hierarchy.
=item B<setClass ($class)>
The argument C<$class> should be either a class name or an instance, it is then used as the root from which to determine the class hierarchy.
=item B<setNodeFilter ($filter_function)>
This method accepts a CODE reference as its C<$filter_function> argument and throws an exception if it is not a code reference. This code reference is used to filter the tree nodes as they are created, the C<$filter_function> is passed the node value extracted from the hash prior to it being inserted into the tree being built. The C<$filter_function> is expected to return the value desired for inclusion into the tree.
=item B<visit ($tree)>
This is the method that is used by Tree::Simple's C<accept> method. It can also be used on its own, it requires the C<$tree> argument to be a Tree::Simple object (or derived from a Tree::Simple object), and will throw and exception otherwise.
The C<$tree> argument which is passed to C<visit> must be a leaf node. This is because this Visitor will create all the sub-nodes for this tree. If the tree is not a leaf, an exception is thrown. We do not require the tree to be a root though, and this Visitor will not affect any nodes above the C<$tree> argument.
=back
=head1 TO DO
=over
=item Improve the C<includeMethods> functionality
I am not sure the tree this creates is the optimal tree for this situation. It is sufficient for now, until I have more of an I<actual> need for this functionality.
=item Add C<includeFullSymbolTable> functionality
This would traverse the full symbol tables and produce a detailed tree of everything it finds. This takes a lot more work, and as I have no current need for it, it remains in the TO DO list.
=back
=head1 BUGS
None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.
=head1 CODE COVERAGE
See the B<CODE COVERAGE> section in L<Tree::Simple::VisitorFactory> for more information.
=head1 SEE ALSO
These Visitor classes are all subclasses of B<Tree::Simple::Visitor>, which can be found in the B<Tree::Simple> module, you should refer to that module for more information.
=head1 AUTHOR
stevan little, E<lt>stevan@iinteractive.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2004, 2005 by Infinity Interactive, Inc.
L<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|