Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Answer: How do I read from a file, that was read from a directory?

( #6736=categorized answer: print w/ replies, xml ) Need Help??

Q&A > files > How do I read from a file, that was read from a directory? contributed by chromatic

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; }

Comment on Answer: How do I read from a file, that was read from a directory?
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2014-12-29 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls