From 395907e53b1ac20f659b3c067e5e4f2b84ba77eb Mon Sep 17 00:00:00 2001 From: Jens Luedicke Date: Sun, 10 Mar 2013 19:32:42 +0100 Subject: [PATCH] M: Pass directory and basename to callback functions. --- lib/File/DirWalk.pm | 11 +++++------ t/2.t | 9 +++++++-- t/4.t | 32 ++++++++++++++++++++++++-------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/lib/File/DirWalk.pm b/lib/File/DirWalk.pm index f89f720..9dd841e 100644 --- a/lib/File/DirWalk.pm +++ b/lib/File/DirWalk.pm @@ -128,7 +128,6 @@ sub entryList { sub walk { my ($self,$path) = @_; - my $currentDir = dirname($path); my $currentBasename = basename($path); my $currentPath = $path; @@ -137,13 +136,13 @@ sub walk { $self->{currentBasename} = $currentBasename; $self->{currentPath} = $path; - if ((my $r = $self->{onBeginWalk}->($path)) != SUCCESS) { + if ((my $r = $self->{onBeginWalk}->($path,$currentDir,$currentBasename)) != SUCCESS) { return $r; } if (-l $path) { - if ((my $r = $self->{onLink}->($path)) != SUCCESS) { + if ((my $r = $self->{onLink}->($path,$currentDir,$currentBasename)) != SUCCESS) { return $r; } @@ -158,7 +157,7 @@ sub walk { $self->{count} = scalar @{$self->{entryList}}; ++$self->{currentDepth}; - if ((my $r = $self->{onDirEnter}->($path)) != SUCCESS) { + if ((my $r = $self->{onDirEnter}->($path,$currentDir,$currentBasename)) != SUCCESS) { return $r; } @@ -183,12 +182,12 @@ sub walk { $self->{currentBasename} = $currentBasename; $self->{currentPath} = $path; - if ((my $r = $self->{onDirLeave}->($path)) != SUCCESS) { + if ((my $r = $self->{onDirLeave}->($path,$currentDir,$currentBasename)) != SUCCESS) { return $r; } --$self->{currentDepth}; } else { - if ((my $r = $self->{onFile}->($path)) != SUCCESS) { + if ((my $r = $self->{onFile}->($path,$currentDir,$currentBasename)) != SUCCESS) { return $r; } } diff --git a/t/2.t b/t/2.t index a55432e..c8169fd 100644 --- a/t/2.t +++ b/t/2.t @@ -14,11 +14,16 @@ foreach my $f qw(1.t 2.t 3.t 4.t) { $dw->setDepth(1); $dw->onFile(sub { - my ($path) = @_; + my ($path,$dir,$basename) = @_; ok(-e $path); ok(-f $path); + ok(-d $dir); + is($dir, "t"); + is($dw->currentDir(), "t"); + is($dw->currentDir(), $dir); + is($dw->currentBasename(), $basename); - if ($dw->currentBasename() eq $f) { + if (($dw->currentBasename() eq $f) and ($basename eq $f)) { return 42; } diff --git a/t/4.t b/t/4.t index a2cee8a..7e5ab3d 100644 --- a/t/4.t +++ b/t/4.t @@ -18,10 +18,12 @@ foreach my $subdir (qw(dir1 dir2 dir3 dir4 dir5)) { $dw = new File::DirWalk(); $dw->onBeginWalk(sub { - my ($path) = @_; + my ($path,$dir,$basename) = @_; ok(-e $path); + is($dw->currentDir(), $dir); + is($dw->currentBasename(), $basename); - if ($dw->currentBasename() eq $subdir) { + if (($dw->currentBasename() eq $subdir) and ($basename eq $subdir)) { return 42; } @@ -31,11 +33,14 @@ foreach my $subdir (qw(dir1 dir2 dir3 dir4 dir5)) { $dw = new File::DirWalk(); $dw->onDirEnter(sub { - my ($path) = @_; + my ($path,$dir,$basename) = @_; ok(-e $path); ok(-d $path); + ok(-d $dir); + is($dw->currentDir(), $dir); + is($dw->currentBasename(), $basename); - if ($dw->currentBasename() eq $subdir) { + if (($dw->currentBasename() eq $subdir) and ($basename eq $subdir)) { is( $dw->count(), 10 ); is( $dw->currentDepth(), 2 ); return 42; @@ -47,11 +52,14 @@ foreach my $subdir (qw(dir1 dir2 dir3 dir4 dir5)) { $dw = new File::DirWalk(); $dw->onDirLeave(sub { - my ($path) = @_; + my ($path,$dir,$basename) = @_; ok(-e $path); ok(-d $path); + ok(-d $dir); + is($dw->currentDir(), $dir); + is($dw->currentBasename(), $basename); - if ($dw->currentBasename() eq $subdir) { + if (($dw->currentBasename() eq $subdir) and ($basename eq $subdir)) { is( $dw->count(), 10 ); is( $dw->currentDepth(), 2 ); return 42; @@ -64,9 +72,14 @@ foreach my $subdir (qw(dir1 dir2 dir3 dir4 dir5)) { $dw = new File::DirWalk(); $dw->onFile(sub { - my ($path) = @_; + my ($path,$dir,$basename) = @_; ok(-e $path); ok(-f $path); + ok(-d $dir); + is($dir, "t/tree/$subdir"); + is($dw->currentDir(), "t/tree/$subdir"); + is($dw->currentDir(), $dir); + is($dw->currentBasename(), $basename); return SUCCESS; }); @@ -77,9 +90,12 @@ foreach my $subdir (qw(dir1 dir2 dir3 dir4 dir5)) { $files = 0; $dw = new File::DirWalk(); $dw->onFile(sub { - my ($path) = @_; + my ($path,$dir,$basename) = @_; ok(-e $path); ok(-f $path); + ok(-d $dir); + is($dw->currentDir(), $dir); + is($dw->currentBasename(), $basename); ++$files; return SUCCESS; });