manishrathi has asked for the wisdom of the Perl Monks concerning the following question:

I am trying a simple code of reading dir content and printing it on cmd prompt. But its not working. What can be problem in here ?
opendir (dir, "C:\Users\Admin\Desktop\Staples files"); @dir_content = readdir(dir); closedir(dir); for $d(@dir_content){ print "$d "; }
This code does not give any error nor does it print output

I have tried it in different ways as follows
opendir dir, "C:\Users\Admin\Desktop\Staples files\"; my @files = readdir(dir); closedir(dir); foreach $file(@files){ print "$file \n"; } print $#files ;
I am getting following errors when I run this code

C:\Users\Admin>perl C:\Users\Admin\Desktop\perl_programs\printDir.pl Scalar found where operator expected at C:\Users\Admin\Desktop\perl_programs\pri ntDir.pl line 6, near "print "$file" (Might be a runaway multi-line "" string starting on line 1) (Do you need to predeclare print?) Backslash found where operator expected at C:\Users\Admin\Desktop\perl_programs\ printDir.pl line 6, near "$file \" (Missing operator before \?) String found where operator expected at C:\Users\Admin\Desktop\perl_programs\pri ntDir.pl line 6, at end of line (Missing semicolon on previous line?) syntax error at C:\Users\Admin\Desktop\perl_programs\printDir.pl line 6, near "p rint "$file " Can't find string terminator '"' anywhere before EOF at C:\Users\Admin\Desktop\p erl_programs\printDir.pl line 6.
open file, "< C:\Users\Admin\Desktop\staples\readfile.txt"; @file_content = <file>; for $f (@file_content){ print "$f \n"; }
This code does not give anu errors. but does not print content of files either.

Replies are listed 'Best First'.
Re: Reading DIR content and printing it
by Corion (Patriarch) on Jul 17, 2011 at 10:35 UTC

    The backslash is special within strings. See perlop. If you had run your code with warnings enabled, Perl would have told you so:

    Unrecognized escape \A passed through at -e line 1. Unrecognized escape \D passed through at -e line 1. Unrecognized escape \S passed through at -e line 1.

    Either properly escape the backslash (see perlop, or use forward slashes to separate the directories.

      Thanks, And why would I not get output, when there is no "\" ?
      open file, "< readfile.txt"; @filecontent = <file>; for $f (@filecontent){ print "$f \n" ; } close file;

        In addition to checking for errors, $f has a newline at its end. Again, if you had used warnings, Perl would have told you so:

        Unsuccessful open on filename containing newline at -e line 1.

        Also, when printing out values, always put delimiters around them so you see whitespace:

        print "[$f]\n";
        check for errors:
        open file, "< readfile.txt" or die "Unable open file: $!";
Re: Reading DIR content and printing it
by Khen1950fx (Canon) on Jul 17, 2011 at 14:13 UTC
    I usually try to recursively read directory content. This script will print to STDOUT each subdirectory and files in each subdirectory of your chosen directory:
    #!/usr/bin/perl use strict; use warnings; my $dir = '/usr/lib/perl5/5.8.8/CGI'; @ARGV = $dir unless @ARGV; use File::Find; find sub { print $File::Find::name, -d && $dir, "\n" }, @ARGV;
    Note that neither opendir, readdir, nor File::Find will print the "contents" of the files, just the names thereof.
Re: Reading DIR content and printing it
by perlaintdead (Scribe) on Jul 18, 2011 at 05:45 UTC

    here's my solution

    opendir(DIR, "\."); @filelist = readdir(DIR); closedir(DIR); $filelist = @filelist; $i = 0; for(;;){ if(defined $filelist[$i]){ print "$filelist[$i]\n"; } else { exit; }; $i++; }