star7 has asked for the wisdom of the Perl Monks concerning the following question:
the subroutine dir_explorer goes through the indicated directory recursively. For each text file in a directory the subroutine textsformat is invoked. Intention: successively: for each textfile of a directory the subroutine textsformat is invoked ,following the new formatted text file has to be inserted into the textfile (see open(FILE, ">>$entry.tab"), which contains all formatted textfiles of a directory.How can I solve the problem as best. Also I want to assign the name of the visited directory as parameter to the subroutine textsformat. Please indicates also the suitable code. I hope you understand my problem.sub textsformat { # variables: initialization ... ... ... my $text =$_[0]; open (MYFILE, "<$text") || die "the file $text is not readable: $!" ; open (OUTPUT, ">$text.txt") || die "can not open $text\.txt with the permission write: $!"; LINE: while(<MYFILE>) { ..... # Algo for formatting a textfile }; close(MYFILE); close(OUTPUT); }; sub dir_explorer { my $entry = $_ ; if (-d $entry) { ... # ignore . and .. opendir(DIR, $entry) or die "Can not open $entry: $!"; open(FILE, ">>$entry.tab") or die "Can not open $entry with the p +ermission write: $!"; while( defined (my $file = readdir DIR) ) { if (-f "$entry/$file") { textsformat("$entry/$file"); # .... }; }; close(FILE); closedir(DIR); }; }; find(\&dir_explorer, "/home/dub/S/programming/perl/dp");
Thank you
best wishesedited: Sun Jun 1 15:55:03 2003 by jeffa - title change (was: good solution ...)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reformatting while traversing directories
by allolex (Curate) on May 31, 2003 at 21:57 UTC | |
by star7 (Novice) on May 31, 2003 at 22:15 UTC | |
by allolex (Curate) on Jun 01, 2003 at 16:54 UTC | |
|
Re: Reformatting while traversing directories
by graff (Chancellor) on Jun 01, 2003 at 05:59 UTC | |
by star7 (Novice) on Jun 01, 2003 at 10:23 UTC |