1Tree::Simple::Visitor::ULsoeardDCiornetcrtiobruytTTerrdeeeeP:(e:3rS)limDpolceu:m:eVnitsaittioorn::LoadDirectoryTree(3)
2
3
4
6 Tree::Simple::Visitor::LoadDirectoryTree - A Visitor for loading the
7 contents of a directory into a Tree::Simple object
8
10 use Tree::Simple::Visitor::LoadDirectoryTree;
11
12 # create a Tree::Simple object whose
13 # node is path to a directory
14 my $tree = Tree::Simple->new("./");
15
16 # create an instance of our visitor
17 my $visitor = Tree::Simple::Visitor::LoadDirectoryTree->new();
18
19 # set the directory sorting style
20 $visitor->setSortStyle($visitor->SORT_FILES_FIRST);
21
22 # create node filter to filter
23 # out certain files and directories
24 $visitor->setNodeFilter(sub {
25 my ($item) = @_;
26 return 0 if $item =~ /CVS/;
27 return 1;
28 });
29
30 # pass the visitor to a Tree::Simple object
31 $tree->accept($visitor);
32
33 # the tree now mirrors the structure of the directory
34
36 This visitor can be used to load a directory tree into a Tree::Simple
37 hierarchy.
38
40 new There are no arguments to the constructor the object will be in its
41 default state. You can use the "setNodeFilter" and "setSortStyle"
42 methods to customize its behavior.
43
44 setNodeFilter ($filter_function)
45 This method accepts a CODE reference as its $filter_function
46 argument and throws an exception if it is not a code reference.
47 This code reference is used to filter the tree nodes as they are
48 created. The function is given the current directory or file being
49 added to the tree, and it is expected to return either true (1) of
50 false (0) to determine if that directory should be traversed or
51 file added to the tree.
52
53 setSortStyle ($sort_function)
54 This method accepts a CODE reference as its $sort_function argument
55 and throws an exception if it is not a code reference. This
56 function is used to sort the individual levels of the directory
57 tree right before it is added to the tree being built. The function
58 is passed the current path, followed by the two items being sorted.
59 The reason for passing the path in is so that sorting operations
60 can be performed on the entire path if desired.
61
62 Two pre-built functions are supplied and described below.
63
64 SORT_FILES_FIRST
65 This sorting function will sort files before directories, so
66 that files are sorted alphabetically first in the list followed
67 by directories sorted alphabetically. Here is example of how
68 that would look:
69
70 Tree/
71 Simple.pm
72 Simple/
73 Visitor.pm
74 VisitorFactory.pm
75 Visitor/
76 PathToRoot.pm
77
78 SORT_DIRS_FIRST
79 This sorting function will sort directories before files, so
80 that directories are sorted alphabetically first in the list
81 followed by files sorted alphabetically. Here is example of how
82 that would look:
83
84 Tree/
85 Simple/
86 Visitor/
87 PathToRoot.pm
88 Visitor.pm
89 VisitorFactory.pm
90 Simple.pm
91
92 visit ($tree)
93 This is the method that is used by the Tree::Simple "accept"
94 method. It can also be used on its own, it requires the $tree
95 argument to be a Tree::Simple object (or derived from a
96 Tree::Simple object), and will throw and exception otherwise.
97
98 The node value of the $tree argument (gotten by calling
99 "getNodeValue") is considered the root directory from which we
100 begin our traversal. We use File::Spec to keep our paths cross-
101 platform, but it is expected that you will feed in a valid path for
102 your OS. If the path either does not exist, or is not a directory,
103 then an exception is thrown.
104
105 The $tree argument which is passed to "visit" must be a leaf node.
106 This is because this Visitor will create all the sub-nodes for this
107 tree. If the tree is not a leaf, an exception is thrown. We do not
108 require the tree to be a root though, and this Visitor will not
109 affect any nodes above the $tree argument.
110
112 <https://github.com/ronsavage/Tree-Simple-VisitorFactory>
113
115 Bugs should be reported via the CPAN bug tracker at
116
117 <https://github.com/ronsavage/Tree-Simple-VisitorFactory/issues>
118
120 See the CODE COVERAGE section in Tree::Simple::VisitorFactory for more
121 information.
122
124 These Visitor classes are all subclasses of Tree::Simple::Visitor,
125 which can be found in the Tree::Simple module, you should refer to that
126 module for more information.
127
129 stevan little, <stevan@iinteractive.com>
130
132 Copyright 2004, 2005 by Infinity Interactive, Inc.
133
134 <http://www.iinteractive.com>
135
136 This library is free software; you can redistribute it and/or modify it
137 under the same terms as Perl itself.
138
139
140
141perl v5.34.0 20T2r2e-e0:1:-S2i1mple::Visitor::LoadDirectoryTree(3)