File::DirWalk - walk through a directory tree and run own code


File-DirWalk documentation  | view source Contained in the File-DirWalk distribution.

Index


NAME

Top

File::DirWalk - walk through a directory tree and run own code

SYNOPSIS

Top

Walk through your homedir and print out all filenames:

	use File::DirWalk;

	my $dw = new File::DirWalk;
	$dw->onFile(sub {
		my ($file) = @_;
		print "$file\n";

		return File::DirWalk::SUCCESS;
	});

	$dw->walk($ENV{'HOME'});

Walk through your homedir and print out all directories:

	use File::DirWalk;

	my $dw = new File::DirWalk;
	$dw->onDirEnter(sub {
		my ($dir) = @_;
		print "$dir\n";

		return File::DirWalk::SUCCESS;
	});

	$dw->walk($ENV{'HOME'});

Walk through your homedir and print out all directories with depth 3:

	use File::DirWalk;

	my $dw = new File::DirWalk;
	$dw->onDirEnter(sub {
		my ($dir) = @_;
		print "$dir\n";

		return File::DirWalk::SUCCESS;
	});

	$dw->setDepth(3);
	$dw->walk($ENV{'HOME'});




DESCRIPTION

Top

This module can be used to walk through a directory tree and run own functions on files, directories and symlinks.

METHODS

Top

new()

Create a new File::DirWalk object

onBeginWalk(\&func)

Specify a function to be be run on beginning of a walk. It is called each time the walk method is called. The directory-name is passed to the given function. Function must return true.

Specify a function to be run on symlinks. The symlink-filename is passed to the given function. Function must return true.

onFile(\&func)

Specify a function to be run on regular files. The filename is passed to the given function when called. Function must return true.

onDirEnter(\&func)

Specify a function to be run before entering a directory. The directory-name is passed to the given function when called. Function must return true.

onDirLeave(\&func)

Specify a function to be run on leaving directory. The directory-name is passed to the given function when called. Function must return true.

onForEach(\&func)

Specify a function to be run on each file/directory within another directory. The name is passed to the function when called. Function must return true.

setDepth($int)

Set the directory depth: By default the directory depth is set to 0.

getDepth

Get the directory depth;

walk($path)

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 true.

All callback-methods expect a function reference as their argument. The directory- or filename is passed to the function as the argument when called. The function must return true, otherwise the recursive walk is aborted and walk returns. You don't need to define a callback if you don't need to.

The module provides the following constants: SUCCESS, FAILED, ABORTED and PRUNE (1, 0, -1, -10) which you can use within your callback code. DirWalk will stop processing the current directory if PRUNE is returned by your callback.

BUGS

Top

Please mail the author if you encounter any bugs.

AUTHOR

Top

Jens Luedicke <jensl@cpan.org> web: http://perldude.de/

CHANGES

Top

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

HISTORY

Top

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.

COPYRIGHT AND LICENCE

Top


File-DirWalk documentation  | view source Contained in the File-DirWalk distribution.