C_Mangano has asked for the wisdom of the Perl Monks concerning the following question:

I know how to use the while (<>){} method of reading from a file, but my question is this. What if ARGV is given a folder name. Then, a subroutine parses all the folders below this folder, until it gets to a file. Now I want to be able to read a line from that file. Any help would be greatly appreciated. Thanks.
  • Comment on How do I read from a file, that was read from a directory?

Replies are listed 'Best First'.
Re: How do I read from a file, that was read from a directory?
by chromatic (Archbishop) on Apr 03, 2000 at 21:33 UTC
    Use the -d test to see if the argument is a directory (perldoc -f -X). Use glob to get a list of all files in that directory. Repeat until you reach a file. (-d $file will be false). Then, open that file and read a line from it. Code might look something like this:
    my $possible_file = shift; my $found_file = find_file($possible_file); { local *INPUT; open(INPUT, $found_file) or die "Can't open $found_file: $!"; my $line = <INPUT>; # do something with $line close INPUT or die "Can't close $found_file. Bogosity: $!"; } sub find_file { my $file = shift; while (-d $file) { # the next line may differ on Win/Mac/Un*x my @files = glob("$possible_file/*.*"); foreach (@files) { find_file($_); } # we're not dealing with a file anymore, so return } return $file; }