file-next icon indicating copy to clipboard operation
file-next copied to clipboard

Problem with File::Next::dirs and descend_filter

Open pelagic opened this issue 7 years ago • 2 comments

Hello Andy,

I use your Module File:Next very often in my daily work and I like it a lot! I never used File::Next::dirs up to now and I think there might be a problem in that specific part of the code.

I need to read all directories within some top-level-diectories, but only one level down. This is my setup:

find /tmp/A /tmp/B -type d
/tmp/A
/tmp/A/z
/tmp/A/z/0
/tmp/A/z/2
/tmp/A/z/1
/tmp/A/y
/tmp/A/x
/tmp/B
/tmp/B/d
/tmp/B/c
/tmp/B/b
/tmp/B/a

When I use this code:

my $iter = File::Next::dirs(
    { descend_filter => sub{0} }
  , @dirs
);
while ( defined (my $trovato = $iter->()) ) {
    say $trovato;
}

I get only the directories I used as parameter:

/tmp/A
/tmp/B

When I use

descend_filter => sub{1}

I get all directories down to the leaves (which is the expected behaviour here).

Am I missing something? Or is this a use case that is rarely used maybe?

Best regards, Imre

Environment:

RHEL 7.0
perl v5.16.3
File::Next::VERSION : 1.12

pelagic avatar Aug 31 '18 11:08 pelagic

I'm not understanding what you're seeing as wrong. In your first case, you're telling descend_filter to never descend, so you only get the starting points. In your second, you're telling descend_filter to always descend, so you get the entire tree. That makes sense to me.

What is it you were expecting differently?

petdance avatar Aug 31 '18 15:08 petdance

I read the description here:

The descend filter is NOT applied to any directory names specified as @starting_points in the constructor.

This seems useful to me when I am looking for files only. So I can decide to search just the starting points or all deeper directories also. Searching for directories, I see a need for a similar feature: I want to see either just the directories immediately within the starting points or I might be interested in all directories through all levels.

pelagic avatar Aug 31 '18 21:08 pelagic