in reply to How to read files in a directory, directory path to be given through CMD

If I intuit your intent properly, there are two changes you need to make:
  1. =foreach needs to be foreach. You have made a syntax error that makes the interpreter think you are entering a documentation mode (see perlpod), thus no useful error message.
  2. Rather than <*>, which operates as a glob, you mean <>, which reads off STDIN.
So a functional version of your code might be
@files = <>; chomp @files; # Since there are trailing newlines foreach $file (@files) { if (-f $file) { print "This is a file: " . $file."\n"; } if (-d $file) { print "\n\nThis is a directory: " . $file."\n"; } }
or, if I wrote it,
use strict; use warnings; my @files = <>; chomp @files; foreach my $file (@files) { if (-f $file) { print "This is a file: $file\n"; } if (-d $file) { print "\n\nThis is a directory: $file\n"; } }
What materials are you using to learn Perl? There are a number of great resources, including some free ones. For thorough coverage of available learning resources, see http://learn.perl.org.

Update: Corrected omitted chomp.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: How to read files in a directory, directory path to be given through CMD
by ckj (Chaplain) on Jun 11, 2012 at 19:12 UTC
    Sorry, but have you run your code? I ran it and on entering path name after running my perl script it says :
    C:\Perl64\bin>perl t1.pl C:\Perl64\bin Can't do inplace edit: C:\Perl64\bin is not a regular file at t1.pl li +ne 20.
      Ahh, so you do not intend STDIN, but rather @ARGV for your input. In that case, you might use
      use strict; use warnings; foreach my $file (@ARGV) { if (-f $file) { print "This is a file: $file\n"; } if (-d $file) { print "\n\nThis is a directory: $file\n"; } }

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Can't do inplace edit: C:\Perl64\bin is not a regular file at t1.pl line 20.

      How can you possibly be getting a error on line 20 of kennethk's code, when neither version of the code he posted has 20 lines?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?