note
2teez
<p>
Hi [Vijay8l],<br>
<i>Am trying to read directory/subdirectories and for files from a path recursively</i><br><br>
Why not use [doc://File::Find] instead of doing all the jobs yourself.<br><br>
<i>..reading file content(usually just 1 line in a file) and substituting space with comma from the line.</i><br> Are you trying to create a Comma-separated values files? If yes then see [cpan://Text::CSV].<br><br>
Just to give a head up, using [doc://File::Find] and substitution function, I did something very close to what you wanted.<br>
<c>
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
my $base_dir = '...'; # put in your base directory
find( \&wanted, $base_dir );
sub wanted {
return if $_ eq '.' or $_ eq '..';
if (-d) {
print " >>> dive into: $_\n" if -d;
}
else {
readout_file($_); ## call subroutine readout_file
}
}
sub readout_file {
my ($filename) = @_;
open my $fh, '<', $filename or die "can't open file:$!";
while (<$fh>) {
chomp;
s/ /,/; ## OR s/ /,/g; if you want
print $_, $/; ## OR any other subroutine you want
}
}
</c>
</p>
<tt>Update:</tt><br>
Seriously, Please look into [cpan://Text::CSV] if csv files are intended.
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-918402">
If you tell me, I'll forget.<br>
If you show me, I'll remember.<br>
if you involve me, I'll understand.<br>
--- Author [unknown to me]
</div></div>
1008518
1008518