[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Recursively visit all files in a directo



On 12:29 PM 12/27/00 Terje Bless <link@tss.no> wrote:

> On 27.12.00 at 11:00, Jim Correia <correia@barebones.com> wrote:
> 
> >What is the easiest way to do this?
> 
> Recursive Decent Parser.

That's what I ended up doing:

sub DirectoryRecurser($$)
{
    my $dir         = shift(@_);
    my $callback    = shift(@_);
    my $path_delim  = ( $OSNAME eq 'MacOS' ? ':' : '/' );
    my $f;
    my @listing;
    
    $dir =~ s/$path_delim$//; # strip of the trailing path delimiter

    opendir(DIR, $dir) || croak "can't opendir $dir";
    @listing = readdir(DIR);
    closedir(DIR);
    
    while ( defined($f = shift(@listing) ) )
    {
        &$callback("$dir$path_delim$f");

        next if ( -l "$dir$path_delim$f" ); # don't follow aliases
        next if ( $f =~ m/\./ );
        next if ( $f =~ m/\.\./ );

        if ( -d "$dir$path_delim$f" )
        {
            DirectoryRecurser("$dir$path_delim$f", $callback);
        }
    }
}

> >I could do it myself but surely there must already be a module
> 
> File::Find

I see that.  I'm alergic to how it sets up package variables for the
callback to use rather than passing the arguments to the callback.  But
it does work :-).

-- 
Jim Correia                                Bare Bones Software, Inc.
correia@barebones.com                     <http://web.barebones.com>

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org