[Omaha.pm] Perl and recursion...

Theodore Katseres tedkat at gmail.com
Tue Nov 15 11:31:41 PST 2005


On 11/15/05, Daniel Linder <dan at linder.org> wrote:
> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm trying to setup a script
> to recursively step through a directory and take action on files and
> directories.  Pretty simple, eh?
>
>  My script runs perfectly well and can handle the files and directory
> entries differently, it is only when I recursively call the subroutine that
> it fails.  The script recursively dives into the sub-directory, but when it
> is done with that directory the recursive call appears to kill the whole
> loop.
>
>  Here is my code -- what am I missing?

It appears that no recursion is happening passed the initial dive. {
ProcFiles("/tmp") }
Check my comments.

<Snip && Replace>
#!/bin/perl -w
use strict;

sub ProcFiles {
    my $dir = shift;

    opendir( DIR, $dir ) or die $!;

    while ( my $file = readdir(DIR) ) {

        next if ( -d "$dir/$file" && $file =~ /^\./ );

        # above makes sure this block of code never gets executed
        if ( -d "$dir/$file" ) {

            printf( "Diving into\"%s/%s\".\n", $dir, $file );

            &ProcFiles("$dir/$file");

            next;

        }

        # your printing files that don't begin with "."
        printf( "Adding\"%s/%s\".\n", $dir, $file );

    }

    closedir(DIR);

}

&ProcFiles("/tmp");


--
Ted Katseres
      ||=O=||


More information about the Omaha-pm mailing list