Here's a small tweak to your initial script which goes some way toward
shedding light on what was going wrong:

#!/usr/bin/env perl

use strict;
use warnings;
use File::Find;
use feature 'say';

my $d = './one';

find sub {
    return if -f;
    say $File::Find::name;
    say "\$File::Find::dir<$File::Find::dir>\n";
}, $d;

The way it works it that `find` is traversing through the directories and
`$File::Find::dir` is the directory it's *in* when it calls your subroutine
on `$File::Find::name` which is inside that directory. When it was sitting
in `/three` it was only called on a non-directory (`three/tst.pl`) and
therefore didn't report that it was in directory `/three`.

Does that clarify things for you?

Andrew


On Sat, Jun 24, 2017 at 9:58 PM, Harry Putnam <rea...@newsguy.com> wrote:

> and...@geekuni.com (Andrew Solomon) writes:
>
> > Hi Harry
> >
> > What do you want your code to do?
> >
>
> Devise a simple test script the counts the number of directories in a
> hierarchy (This is building toward a more complex script in the end).
> But taking small steps in an effort to really understand what is
> happening.
>
> >>   find sub {
> >>     return if -f;
> >>     print "\$File::Find::dir<$File::Find::dir>\n";
> >>   }, $d;
>
> Seemed to me, would do that.  Skip -f type files and print all
> directory names.
>
> Instead I see:
>
> >> Output:
> >>   reader > ./tst.pl
> >>   $File::Find::dir<./one>
> >>   $File::Find::dir<./one>
> >>   $File::Find::dir<./one/two>
>
> That is the parent directory is printed twice, the second level is
> printed as I'd expect. The third level is not printed at all.
>
> I see on reflection that I should have just left any code concerning
> -f files clear out and simply selected -d type files and printed them,
> And more importantly changed what I'm printing... instead of
> File::Find::dir what at first blush seemed like the right choice but
> once you've selected only -d type then File::Find::name shows the
> whole path:
>
>   find sub {
>      if (-d) {
>         print  "\$File::Find::name<$File::Find::name>\n";
>      }
>   }, $d;
>
> Which does return what I would expect... so now I can build on that
> and try to get a little closer to something I can use.
>
>   $File::Find::name<./one>
>   $File::Find::name<./one/two>
>   $File::Find::name<./one/two/three>
>
> I'm not really clear yet on why my first code did not cover all levels
> of the hierarchy but the second stab does.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
Andrew Solomon

Mentor@Geekuni http://geekuni.com/
http://www.linkedin.com/in/asolomon

Reply via email to