A
Adam
A fairly normal thing to do in Perl is to read input from the files
named in the command arguments and process line by line.
while (<>) { process($_) ; }
But I want to recurse down directories the way grep -R works. So far
I've come up with this:
while (@ARGV>0) {
$filename = shift(@ARGV);
print ($filename);
if ( -d $filename ) {
@list = `ls $filename` ;
@list1 = map {chomp; $_ = $filename . "/" . $_ } @list ;
unshift(@ARGV, @list1) ;
}
elsif ( -e $filename ) {
open(INPUTFILE, $filename) ;
while($line = <INPUTFILE>) {
process($line) ;
}
close(INPUTFILE) ;
}
}
It works but I can't believe it's the best way to do it. Any better
suggestions?
named in the command arguments and process line by line.
while (<>) { process($_) ; }
But I want to recurse down directories the way grep -R works. So far
I've come up with this:
while (@ARGV>0) {
$filename = shift(@ARGV);
print ($filename);
if ( -d $filename ) {
@list = `ls $filename` ;
@list1 = map {chomp; $_ = $filename . "/" . $_ } @list ;
unshift(@ARGV, @list1) ;
}
elsif ( -e $filename ) {
open(INPUTFILE, $filename) ;
while($line = <INPUTFILE>) {
process($line) ;
}
close(INPUTFILE) ;
}
}
It works but I can't believe it's the best way to do it. Any better
suggestions?