At 12:28 PM 5/1/2001, you wrote:
>sub wanted {
> @FILES = ();
> if ( $File::Find::name =~ /_boot\.js$/ )
> {
> push @FILES, "$File::Find::name\n";
> }
>
> print (@FILES);
>
> open(BOOT_FILES,">boot_files.txt");
> print BOOT_FILES @FILES;
> close BOOT_FILES;
>}
>
>Kaustav
Well, you have to call the 'wanted' sub as part of a call to find. You'll
want to say something like this at the top of your program:
use File::Find;
find \&wanted, "/dir/to/look/for/files/in";
sub wanted {
# put the body of your sub here.
}
Now, your sub will be run each time that the subroutine find 'finds' a
file. So if you want to print all of the files, you should push all of the
names into an array that doesn't get initialized at the top of the sub. In
that case, the array is emptied each time, and then one filename pushed in,
then the contents of the array (one element) printed to STDOUT, then the
file opened (truncating it to 0 bytes,) the contents of the array written
to it, and the file closed. The whole process is repeated each time you
come to a new file.
What you want to do is this. Move all of the file stuff out of the wanted
sub, as well as the initialization of the array, leaving only the if
statement really. You want to initialize the array once, before you start
finding files, push all of the names you want into that array as you are
finding them, and then print all the stuff out the file when you are
done. Something like this:
use strict;
use File::Find;
my @files = ();
find \&wanted, "/path/to/search";
# The "... or die ..." below is always a good idea.
# You never know when you won't be able to open a file up.
open FILE, "> outfile" or die "Can't open outfile: $!\n";
print FILE @files;
close FILE;
# Put your wanted sub down here, minus the code you need to take out.
And that's it. I hope this helps. I think the only thing that you needed
to realize is that this sub isn't run once after you have found all the
files, but each time you come across one. As such, it can be called MANY
times, so you want to keep it as short and sweet as possible, not to
mention use any available speed hacks. (But don't worry about those now.)
Thank you for your time,
Sean.