In the below code, I am trying to read directory and put all directory names into the text file. Here's a catch. When I try to count and read lines in the generated text file it's not working. But, if I try to parse a text file which is having such information it's working. Please help me to understand where it's going wrong.
use strict; use warnings; use Cwd; my $cwd = getcwd(); #inputs# my $dir = "D:\\Temp"; my $fff; my @dir = (); my $contilog = "$cwd"."\\input.txt"; open(LOG,">",$contilog) || die "Cannot create log file ...."; opendir (DIR,$dir); @dir=readdir(DIR); closedir(DIR); @dir = sort(@dir); foreach $fff (@dir){ write_log("$fff"); }
here my code is not working
my $input = "$contilog";
If I uncommment below line and run its working
#my $input = "$cwd"."\\input1.txt";
open my $fh, '<', $input or die "unable to open file: $!"; # read it line by line with while, using the $. variable to # show the line number (instead of your own counter). while( <$fh> ) { print "$_\n"; # LINE NO: LINE } print "$contilog\n"; print "$input\n"; print "Total lines was $.\n"; sub write_log{ print LOG @_; print LOG "\n"; } sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }
In reply to Problem with reading generated text file by Akshit
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |