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 argu‐
46 ment and throws an exception if it is not a code reference. This
47 code reference is used to filter the tree nodes as they are cre‐
48 ated. 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 func‐
56 tion is used to sort the individual levels of the directory tree
57 right before it is added to the tree being built. The function is
58 passed the the current path, followed by the two items being
59 sorted. The reason for passing the path in is so that sorting oper‐
60 ations 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 Tree::Simple's "accept" method.
94 It can also be used on its own, it requires the $tree argument to
95 be a Tree::Simple object (or derived from a Tree::Simple object),
96 and will throw and exception otherwise.
97
98 The node value of the $tree argument (gotten by calling "getNode‐
99 Value") is considered the root directory from which we begin our
100 traversal. We use File::Spec to keep our paths cross-platform, but
101 it is expected that you will feed in a valid path for your OS. If
102 the path either does not exist, or is not a directory, then an
103 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 None that I am aware of. Of course, if you find a bug, let me know, and
113 I will be sure to fix it.
114
116 See the CODE COVERAGE section in Tree::Simple::VisitorFactory for more
117 inforamtion.
118
120 These Visitor classes are all subclasses of Tree::Simple::Visitor,
121 which can be found in the Tree::Simple module, you should refer to that
122 module for more information.
123
125 stevan little, <stevan@iinteractive.com>
126
128 Copyright 2004, 2005 by Infinity Interactive, Inc.
129
130 <http://www.iinteractive.com>
131
132 This library is free software; you can redistribute it and/or modify it
133 under the same terms as Perl itself.
134
135
136
137perl v5.8.8 20T0r5e-e0:7:-S1i3mple::Visitor::LoadDirectoryTree(3)