grep DONE: File/Find.pm
####
File::Find::find( sub { ... goto My::Package::FOUND ... } ... );
My::Package::FOUND:
....
##
##
BEGIN {
my( $dot, $up )= ( File::Spec->curdir(), File::Spec->updir() );
sub SearchDirs {
my( $path, $subdir )= @_;
$path= File::Spec->catdir( $path, $subdir );
chdir $subdir or die "Can't chdir, $path: $!\n";
opendir D, $dot or die "Can't opendir, $path: $!\n";
my @files= grep $_ ne $dot && $_ ne $up, readdir D;
closedir D;
for( @files ) {
my $recurse= ! -l $_ && -d _;
# ...
SearchDirs( $path, $_ ) if $recurse;
}
chdir $up or die "Can't chdir .. from $path: $!\n";
}
}
##
##
#!/usr/bin/perl -w
use strict;
use File::Spec;
# Globals:
my( $FiNode, $FnLinks, @Found );
Main( @ARGV );
exit( 0 );
BEGIN {
my( $dot, $up )= ( File::Spec->curdir(), File::Spec->updir() );
sub SearchDirs {
my( $path )= @_;
my @depth= ".";
my @todo= ( ".", $path );
while( @todo ) {
my $path= shift @todo;
my $subdir= shift @todo;
while( $path ne $depth[-1] ) {
chdir $up or die "Can't chdir .. from $depth[-1]: $!\n";
pop @depth;
}
$path= File::Spec->catdir( $path, $subdir );
push @depth, $path;
chdir $subdir or die "Can't chdir, $path: $!\n";
opendir D, $dot or die "Can't opendir, $path: $!\n";
my @files= grep $_ ne $dot && $_ ne $up, readdir D;
closedir D;
for( @files ) {
push @todo, $path, $_ if ! -l $_ && -d _;
if( $FiNode == ( lstat _ )[1] ) {
push @Found, File::Spec->catfile( $path, $_ );
return if --$FnLinks < 1;
}
}
}
}
}
sub Main {
die "Usage: file dir\n" if 2 != @_;
my( $fpath, $dir )= @_;
( $FiNode, $FnLinks )= ( lstat $fpath )[1,3];
if( 1 == $FnLinks ) {
print $fpath, $/;
} else {
SearchDirs( $dir );
print $_, $/ for @Found;
}
}