
Zhris
User
Sep 11, 2012, 12:35 PM
Post #4 of 8
(797 views)
|
|
Re: [ravi.joshi53] Reading all the files from a directory
[In reply to]
|
Can't Post
|
|
Hi, As suggested by Laurent_R, glob can be used to neatly read the contents of a directory:
my $dir_path = $ARGV[0]; while (<"$dir_path/*">) { print "$_\n"; } Rather than telling you everything thats wrong with your code, here is an example that prints the first line of every file in the directory supplied. Possibly compare it against your own code to see where you have gone wrong:
#!/usr/bin/perl use strict; use warnings; my $dir_path = $ARGV[0]; opendir my $dh, $dir_path or die "cannot open dir $dir_path: $!"; foreach my $file_path ( map { "$dir_path/$_" } grep { -f "$dir_path/$_" } readdir $dh ) { open my $fh, '<', $file_path or die "cannot open file $file_path: $!"; while (my $line = <$fh>) { chomp $line; print "$line\n"; last; } close $fh; } closedir $dh; Chris
(This post was edited by Zhris on Sep 11, 2012, 12:50 PM)
|