File::DirWalk - walk through a directory tree and run callbacks on files, symlinks and directories.
use File::DirWalk; my $dw = File::DirWalk->new;
Walk through your homedir and print out all filenames:
$dw->onFile(sub { my ($file) = @_; print "$file\n"; return SUCCESS; }); $dw->walk($ENV{'HOME'});
Walk through your homedir and print out all directories:
$dw->onDirEnter(sub { my ($path) = @_; print "$path\n"; return SUCCESS; }); $dw->walk($ENV{'HOME'});
Walk through your homedir and print out all directories with depth 3:
$dw->onDirEnter(sub { my ($path) = @_; print "$path\n"; return SUCCESS; }); $dw->setDepth(3); $dw->walk($ENV{'HOME'});
This module can be used to walk through a directory tree and run own functions on files, directories and symlinks.
Create a new File::DirWalk object. The constructor takes no arguments.
Specify a function to be be run on beginning of a walk.
Specify a function to be run on symlinks.
Specify a function to be run on regular files.
Specify a function to be run before entering a directory.
Specify a function to be run when leaving a directory.
Set the directory traversal depth. Once the specified directory depth has been reached, the walk method returns. The default value is 0. Precondition: The value has to be positive. The method will die if called with a negative value.
walk
Returns the user-specified directory traversal depth. The default value is 0.
Returns the current directory traversal depth.
Returns the directory part of the current path.
$dw->onBeginWalk(sub { my ($path) = @_; print "path: " . $path, "\n"; # /usr/bin/perl print "directory: " . $dw->currentDir(), "\n"; # /usr/bin return SUCCESS; });
Returns the current path. The string is identical to the $path argument passed to the callback:
$dw->onBeginWalk(sub { my ($path) = @_; print "path: " . $path, "\n"; # /usr/bin/perl print "directory: " . $dw->currentPath(), "\n"; # /usr/bin/perl return SUCCESS; });
Returns the current base name of the current path:
$dw->onBeginWalk(sub { my ($path) = @_; print "path: " . $path, "\n"; # /usr/bin/perl print "directory: " . $dw->currentBasename(), "\n"; # perl return SUCCESS; });
Returns the number of elements wthin the current directory. Excludes . and ..
Returns an array reference to the elements wthin the current directory. Excludes . and ..
Begin the walk through the given directory tree. This method returns if the walk is finished or if one of the callbacks doesn't return SUCCESS. If the callback function returns PRUNE, walk will skip to the next element within the current directory hierarchy. You can use PRUNE to exclude files or folders:
$dw->onBeginWalk(sub { my ($path) = @_; if ($path =~ /ignore/) { return PRUNE; } return SUCCESS; });
All callback-methods expect a function reference as their argument. The current path is passed to the callback function.
The callback function must return SUCCESS, otherwise the recursive walk is aborted and walk returns. Furthermore, walk expects a numeric return value.
File::DirWalk exports the following predefined constants as return values:
Please mail the author if you encounter any bugs. The most recent development version can be found on GitHub: https://github.com/nullmedium/File-DirWalk
Version 0.5: bugfixes, improved testing, new currentDepth() method.
Version 0.4: add more methods, better testing, more documentation.
Version 0.3: add PRUNE constant. add option to specify the directory depth.
Version 0.2: platform portability fixes and more documentation
Version 0.1: first CPAN release
I wrote DirWalk.pm module for use within my 'Filer' file manager as a directory traversing backend and I thought it might be useful for others. It is my first CPAN module.
Jens Luedicke <jensl@cpan.org>
Copyright (c) 2005-2013 Jens Luedicke <jensl@cpan.org>.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
To install File::DirWalk, copy and paste the appropriate command in to your terminal.
cpanm
cpanm File::DirWalk
CPAN shell
perl -MCPAN -e shell install File::DirWalk
For more information on module installation, please visit the detailed CPAN module installation guide.