Tree::...ryTree(3pm)

πŸ“¦
version 0.16 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 the setNodeFilter and setSortStyle methods. That's the whole thing.
  • setNodeFilter ($filter_function) β€” this one takes a CODE reference as its $filter_function argument, 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_function argument, 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::Simple accept method calls. You can also use it all on its own, but it requires the $tree argument 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 $tree argument passed to visit must 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 $tree argument. 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 2004, 2005 by Infinity Interactive, Inc.

<http://www.iinteractive.com>

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