http://www.perlmonks.org?node_id=1008528


in reply to process a file and reading a line and passing the values to another sub function

Hi Vijay8l,
Am trying to read directory/subdirectories and for files from a path recursively

Why not use File::Find instead of doing all the jobs yourself.

..reading file content(usually just 1 line in a file) and substituting space with comma from the line.
Are you trying to create a Comma-separated values files? If yes then see Text::CSV.

Just to give a head up, using File::Find and substitution function, I did something very close to what you wanted.

#!/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 } }

Update:
Seriously, Please look into Text::CSV if csv files are intended.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
  • Comment on Re: process a file and reading a line and passing the values to another sub function
  • Download Code