Tree::...ryTree(3pm)
π NAME π
Tree::Simple::Visitor::LoadDirectoryTree β a Visitor that loads the whole entire contents of a directory into a Tree::Simple object, no cap ποΈβ¨
πΈ SYNOPSIS πΈ
use Tree::Simple::Visitor::LoadDirectoryTree;
# create a Tree::Simple object whose
# node is path to a directory
my $tree = Tree::Simple->new("./");
# create an instance of our visitor
my $visitor = Tree::Simple::Visitor::LoadDirectoryTree->new();
# set the directory sorting style
$visitor->setSortStyle($visitor->SORT_FILES_FIRST);
# create node filter to filter
# out certain files and directories
$visitor->setNodeFilter(sub {
my ($item) = @_;
return 0 if $item =~ /CVS/;
return 1;
});
# pass the visitor to a Tree::Simple object
$tree->accept($visitor);
# the tree now mirrors the structure of the directory
π¦ DESCRIPTION π¦
okay so bestie, picture this: you have a directory, right? and you want it to just be a Tree::Simple hierarchy. That's it, that's the whole job. This visitor takes a directory tree and loads it straight into a Tree::Simple structure. It's giving magic, honestly. β¨
π METHODS π
newβ no arguments, babes. The constructor just hands you an object in its default state, and then you customize the vibe with thesetNodeFilterandsetSortStylemethods. That's the whole thing.setNodeFilter ($filter_function)β this one takes a CODE reference as its$filter_functionargument, and it will throw an exception if you hand it anything that is not a code reference π¨. So what does this code ref even do? It filters the tree nodes as they get created, okay? Your function gets passed the current directory or file that's being added to the tree, and it's supposed to return either true (1) or false (0) β that return value is what decides whether that directory gets traversed or that file gets added to the tree. Filtering era fr fr.setSortStyle ($sort_function)β this method takes a CODE reference as its$sort_functionargument, and throws an exception if it's not a code reference, same energy as above π. This function sorts the individual levels of the directory tree right before they're added to the tree being built. Here's the lil detail that matters: the function is passed the current path, followed by the two items being sorted. Why does it get the path, you ask? So that your sorting operations can work on the entire path if you want. iykyk π«
Two pre-built functions are supplied, and yes we're describing both of them below.
β SORT_FILES_FIRST β
This sorting function sorts files before directories, so files get sorted alphabetically first in the list, followed by directories sorted alphabetically. Here's what that looks like:
Tree/
Simple.pm
Simple/
Visitor.pm
VisitorFactory.pm
Visitor/
PathToRoot.pm
π SORT_DIRS_FIRST π
This sorting function sorts directories before files, so directories get sorted alphabetically first in the list, followed by files sorted alphabetically. Here's what that looks like:
Tree/
Simple/
Visitor/
PathToRoot.pm
Visitor.pm
VisitorFactory.pm
Simple.pm
visit ($tree)β this is the method that the Tree::Simpleacceptmethod calls. You can also use it all on its own, but it requires the$treeargument to be a Tree::Simple object (or something derived from a Tree::Simple object), and it will throw an exception otherwise, periodt. π«Ά
The node value of the $tree argument (which you get by calling getNodeValue) is treated as the root directory where the traversal begins. We use File::Spec to keep our paths cross-platform, but it's expected that you'll feed in a valid path for your OS β that part's on you, bestie. And if the path either does not exist, or is not a directory, then an exception gets thrown. π
Heads up: the$treeargument passed tovisitmust be a leaf node. Why? Because this Visitor creates all the sub-nodes for this tree, so 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$treeargument. Slay.
π¦ Repository π¦
<https://github.com/ronsavage/Tree-Simple-VisitorFactory>
π SUPPORT π
Bugs should be reported via the CPAN bug tracker at
<https://github.com/ronsavage/Tree-Simple-VisitorFactory/issues>
π§ CODE COVERAGE π§
See the CODE COVERAGE section in Tree::Simple::VisitorFactory for more information.
π SEE ALSO π
These Visitor classes are all subclasses of Tree::Simple::Visitor, which can be found in the Tree::Simple module β you should refer to that module for more information. Go read it, it's iconic. π
π AUTHOR π
stevan little, <stevan@iinteractive.com>
π COPYRIGHT AND LICENSE π
Copyright 2004, 2005 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay that's the whole page bestie, you ate that π mwah π β this was perl v5.36.0, dated 2022-11-19, and it lives in the User Contributed Perl Documentation. byeee π¦β¨
π¦ where this page comes from
- debian bookworm: libtree-simple-visitorfactory-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtree-simple-visitorfactory-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtree-simple-visitorfactory-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtree-simple-visitorfactory-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtree-simple-visitorfactory-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms