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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.