diff --git a/lib/File/DirWalk.pm b/lib/File/DirWalk.pm index 45b9637..ecbc0ca 100644 --- a/lib/File/DirWalk.pm +++ b/lib/File/DirWalk.pm @@ -87,10 +87,19 @@ sub onDirLeave { sub setDepth { my ($self,$v) = @_; + if ($v < 0) { + croak("Directory depth is negative: $v"); + } + $self->{depth} = $v; } sub getDepth { + my ($self) = @_; + return $self->{depth}; +} + +sub currentDepth { my ($self) = @_; return $self->{depthCount}; } @@ -268,10 +277,16 @@ Specify a function to be run when leaving a directory. Set the directory traversal depth. Once the specified directory depth has been reached, the C method returns. The default value is 0. +Precondition: The value has to be positive. The method will die +if called with a negative value. =item getDepth -Returns the directory traversal depth. +Returns the user-specified directory traversal depth. The default value is 0. + +=item currentDepth + +Returns the current directory traversal depth. =item currentDir @@ -369,7 +384,7 @@ version can be found on GitHub: L =head1 CHANGES -Version 0.5: bugfixes. +Version 0.5: bugfixes, improved testing, new currentDepth() method. Version 0.4: add more methods, better testing, more documentation. diff --git a/t/1.t b/t/1.t index f17b164..0029ee9 100644 --- a/t/1.t +++ b/t/1.t @@ -11,6 +11,12 @@ $dw = new File::DirWalk(); ok( ref($dw) eq 'File::DirWalk' ); +is ($dw->getDepth(), 0); +dies_ok { $dw->setDepth(-1) }; +is ($dw->getDepth(), 0); +ok ($dw->setDepth(1)); +is ($dw->getDepth(), 1); + ok ($dw->setHandler(onBeginWalk => sub { SUCCESS })); ok ($dw->setHandler(onLink => sub { SUCCESS })); ok ($dw->setHandler(onFile => sub { SUCCESS }));