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


NAME

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


SYNOPSIS

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

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


METHODS

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.

onLink(\&func)

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

Please mail the author if you encounter any bugs.


AUTHOR

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


CHANGES

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

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

Copyright (c) 2005-2006 Jens Luedicke. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.