in reply to Navigating Dierctories Recursively

Would Find::File help here?

Also, using the OO interfaces IO::Dir and IO::File may be helpful.

And, taking your code as it is and fixing it ...

sub readindirs { my ($dirname) = @_; opendir(NEWDIR, $dirname) or die $!"; my @newfiles = grep { $_ ne '.' and $_ ne '..' } readdir NEWDIR; closedir NEWDIR; foreach my $name (@newfiles) { if (-d $name) { readindirs($name); } else { parse_sub_routine($name); } } }

The problem you were having was that you were clobbering @newfiles because you weren't scoping it to each invocation of the subroutine. Next time, add use strict; to the top of your script and it will help.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: Navigating Dierctories Recursively
by cens (Novice) on Oct 16, 2003 at 21:05 UTC
    Hmmm... I tried fixing the code as you suggested, and ended up with the same problem. I goes into one directory and no further than that level. I am trying to avoid using the modules, because I am on the road and need to email this to someone who does not really know perl. I am getting the problem where it reads in the directory the first time, and every other time treats the directory like a file (as if the if (-d $name) piece was failing).

    I am running the following:

    #!/usr/bin/perl; use strict; sub readindirs { my ($dirname) = @_; print "$dirname -> dirname \n"; opendir(NEWDIR, $dirname) or die $!; my @newfiles = grep { $_ ne '.' and $_ ne '..' } readdir NEWDIR; closedir NEWDIR; foreach my $name (@newfiles) { if (-d $name) { print "name:: $name \n"; readindirs($name); } else { print "parse file : $name \n"; #&parse_file($name); } } } &readindirs('.');

    Which results in this:

    . -> dirname name:: about about -> dirname parse file : aic3.pl parse file : contact parse file : coredocs parse file : footer.inc parse file : index.html parse file : nav.js parse file : organization parse file : overview parse file : programs parse file : rightnav.inc parse file : template.html parse file : top.inc parse file : about2.zip parse file : aic.pl parse file : aictemp.pl
    Of which contact, coredocs, organization, overview, and programs are directories.

    I am doing this on a windows distr. and am not really a windows person, I know that there are a few differences between *nix and windows perl, is this one of them? The other thing, which is purely for my own interest, is how is  if (-d $name) better/worse than (opendir(DIR, $name)).

    Thanks a lot for your help. If I can't figure it out soon, I think that I will go to the modules.

      *smacks head repeatly against keyboard* This is why I use modules whenever possible.
      my @newfiles = map { File::Spec->catfile($dirname, $_) } grep { $_ ne '.' and $_ ne '..' } readdir NEWDIR;

      You gotta prepend the directory name onto what you read from the DIR handle.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        D'oh ... of course I should have seen that, I am really not paying attention... thanks a lot for the help... its the Sox game tonight... distracting me from work ;)...