[ale] Perl Problem
Mike Fletcher
fletch at phydeaux.org
Tue Oct 13 22:16:43 EDT 1998
Terry Lee Tucker <terry at esc1.com> writes:
> The script below is a portion of a larger script. I'm having a problem
> with the prune variable. I only want File::Find to look in the directory
> I pass as an argument for files that match the pattern defined in the
> "wanted" subroutine. According to my "Programming Perl" book by
> O'Reilly, I should set the variable $File::Find::prune to true to
> prevent find descending into subdirectories. When I set this variable, I
> get nothing at all back. I know there are files that match this pattern
> in the directory I am passing to find, but I get nothing back. If I set
> the variable to zero, I get files from the /esc/rnd/prog directory, as
> well as all the subdirectories below, which is not what I want. Am I
> doing something wrong here?
Yes. :)
First, the first time your wanted sub gets called, it'll be
with the directory you're interested in. Since you're setting the
prune variable right off, it never recurses into the directory. You
can see this behavior with (this is all one line):
$ perl -MFile::Find -e 'find(sub{$File::Find::prune=1;print "$File::Find::dir/$_\n"}, "/var")'
/var/.
$
Second, if you just want to look at one specific directory
without recursing you should be using opendir/readdir rather than
File::Find. You should be doing something more along the lines of:
opendir( PROG, "/$MNT/$DIR/prog" )
or die "opendir: $!";
@files = grep /\.p$/, readdir( PROG );
print "Files:\n", join( "\n", @files ), "\n";
closedir( PROG );
Thirdly, your pattern does a lot of unecessary work. If you
just want .p at the end of line, just match /\.p$/ without ".*".
--
Fletch | __`'/|
fletch at phydeaux.org | "I drank what?" -- Socrates \ o.O'
678 443-6239(w) | =(___)=
| U
More information about the Ale
mailing list