richill has asked for the wisdom of the Perl Monks concerning the following question:
Thanks for your earlier help with the find module and the file tests.
I am getting a list of text files using the find module and the file tests.
Im saving the paths to these files into an array, I then want to open them for writing using a file handle.
So I use a foreach statement to loop through the array and open a file handle for each file, i then use a while loop on the filehandle to read each line at a time. But it doesnt work. I think the file can be accessed because i get no error messages. What is wrong.
Japhy has replied saying the code works on his machine. I'm using osx and perl, could that be a problem.
No the files only contained no data. doh.
#!/usr/local/bin/perl -w # print warnings use strict; # install all three strictures # program to open files in a directory and change directory paths fro +m oldpath to $newpath # use stat function to mke file handles available $|++; # force auto flush of output buffer use File::Find; use Cwd; use File::Path; my @ListofFiles; my $Start_dir = shift or die "usage: $0 <start_dir>\n"; #define string directory as <ST +DIN> # scan recursivley from the start directory unless ( -d $Start_dir ) { die "Start directory .'$Start_dir' . is not a directory.\n"; } sub wanted { my $fileopened = $File::Find::name; my $fileout = "$fileopened.NEW2"; if ( -e $fileopened ) { #print "Found $File::Find::name\n"; } if ( -T $fileopened ) { #print "$File::Find::name is a DATA file\n"; push( @ListofFiles, $fileopened ); } else { #print "Didn't OPEN $File::Find::name\n"; } } sub searchfiles () { my $a; #print "$fileout\n"; foreach $a (@ListofFiles) { print "$a\n"; ( open FILEOPENED, "$a" ) or die "couldn't open the file $!" +; while (<FILEOPENED>) { print "file is opened for reading"; } close(FILEOPENED); } } find( { wanted => \&wanted, no_chdir => 1 }, $Start_dir ); searchfiles();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using filehandle to open files listed in array
by japhy (Canon) on May 14, 2006 at 12:55 UTC | |
by richill (Monk) on May 14, 2006 at 13:36 UTC | |
|
Re: using filehandle to open files listed in array
by merlyn (Sage) on May 14, 2006 at 15:39 UTC | |
by richill (Monk) on May 14, 2006 at 20:17 UTC | |
by jwkrahn (Abbot) on May 14, 2006 at 20:41 UTC |